2013-08-07 62 views
1

我正在寻找如何在Get请求中传递url时如何转义/编码特殊字符。如何在下面的代码中对网址进行编码?

public static void sendData(String strval) throws IOException{ 
    String doSend="https://myhost.com/views?strval="+strval; 
    HttpClient httpclient = new DefaultHttpClient(); 
try { 
    System.out.println("inside try"); 
    URIBuilder builder = new URIBuilder(); 
    System.out.println("builder="+builder); 

    builder.setScheme("http"); 
    builder.setHost("myhost.com").setPath("/views?"); 
    builder.addParameter("strval", strval); 
    System.out.println("add param,sethost,setpath complete"); 

    URI uri = builder.build(); 
    System.out.println("uri="+uri); 


    HttpGet httpget = new HttpGet(uri); 
    System.out.println("httpGet"+httpget); 

    HttpResponse response = httpclient.execute(httpget); 
    System.out.println(response.getStatusLine().toString()); 

    if (response.getStatusLine().getStatusCode() == 200) { 
     String responseText = EntityUtils.toString(response.getEntity()); 
     System.out.println("responseText="+responseText); 
     httpclient.getConnectionManager().shutdown(); 
    } else { 
     System.out.println("Server returned HTTP code " 
       + response.getStatusLine().getStatusCode()); 
    } 
    } catch (java.net.URISyntaxException bad) { 
    System.out.println("URI construction error: " + bad.toString()); 
    } 
    catch(Exception e){ System.out.println("e.getMessage=>"+e.getMessage()); } 

} 

我在调用getRequest网址打印了这一点.. 产生的输出字符串是这样的:这是嵌入一些wied charactrers

http://myhost.com/views%3strval=?strval=http%3A%2F%2Fcnn.com,http%3A%2F%2Fespn.com 

我需要的输出是这样的:

http://myhost.com/views?strval=http://cnn.com,http://espn.com 

有人可以帮我解决,我该如何编码特殊字符?在setpath中? 也 你能帮我修改这段代码吗?

+0

我认为在我们的解码网址中有一些错误。像这里“http%3A%2F%2”那样应该是“http%3A%2F%2F”。 – d0x

+0

谢谢!当我在这里粘贴和编辑时,这是一个错字。 – smiley

+0

Np(p.s.你也可以upvote评论,如果他们帮助你) – d0x

回答

0

您可以使用java.net.URLEncoder

像这样:

public static void main(final String[] args) throws UnsupportedEncodingException 
{ 
    final String encode = URLEncoder.encode("http://www.test.de/ -", "UTF8"); 
    System.out.println(encode); 

    final String decode = URLDecoder.decode(encode, "UTF8"); 
    System.out.println(decode); 

    final String decodeESPN = URLDecoder.decode("http://myhost.com/views%3Fstrval=?strval=http%3A%2F%2Fcnn.com,http%3A%2F%2Fespn.com", "UTF8"); 
    System.out.println(decodeESPN); 
} 

它会打印您:

http%3A%2F%2Fwww.test.de%2F+- 
http://www.test.de/ - 
http://myhost.com/views?strval=?strval=http://cnn.com,http://espn.com 

调整你有你的例子做:

在你的榜样,你可以打印解码后的URL像这样:

System.out.println("httpGet "+ URLDecoder.decode(httpget, "UTF8")); 

备选:

有什么理由让你使用urlbuilder?你也可以做这样的:

public static void sendData(String strval) throws IOException 
{ 
    String doSend = "https://myhost.com/views?strval=" + strval; 
    HttpClient httpclient = new DefaultHttpClient(); 
    try 
    { 
     HttpGet httpget = new HttpGet(doSend); 

     HttpResponse response = httpclient.execute(httpget); 

     if (response.getStatusLine().getStatusCode() == 200) 
     { 
      String responseText = EntityUtils.toString(response.getEntity()); 
      httpclient.getConnectionManager().shutdown(); 
     } 
     else 
     { 
      System.out.println("Server returned HTTP code " + response.getStatusLine().getStatusCode()); 
     } 
    } 
    catch (java.net.URISyntaxException bad) 
    { 
     System.out.println("URI construction error: " + bad.toString()); 
    } 
    catch (Exception e) 
    { 
     System.out.println("e.getMessage=>" + e.getMessage()); 
    } 

} 
+0

问题是,我该如何使用这个java.net.URLEncoder与URI生成器? builder.setHost( “myhost.com”)的setpath( “/视图strval =?”)。 – smiley

+0

另外,你可以指出如何在上面写的代码中做到这一点吗? – smiley

+0

你需要使用网址生成器吗?或者这种“替代”对你有好处? – d0x

0

我不完全理解你的问题,但你的问题的一部分是,你没有

.setPath("/views?strval=");

当它应该是

.setPath("/views");

我希望能帮助你一些。