如何在下面的代码中对url进行编码?

时间:2013-08-07 17:37:02

标签: java http request

我正在寻找在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网址中打印出来。 产生的输出字符串是这样的:这是嵌入一些有害的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? 也 你能帮我修改一下这段代码。

2 个答案:

答案 0 :(得分: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());
    }

}

答案 1 :(得分:0)

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

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

什么时候应该

.setPath("/views");

我希望这可以帮到你。