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 url中打印出来。产生的输出字符串是这样的:这是嵌入一些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

有人可以帮我解决,我如何编码特殊字符,如?在设置路径中?另外你能帮我修改这段代码吗?

4

2 回答 2

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());
    }

}
于 2013-08-07T17:38:50.143 回答
0

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

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

什么时候应该

.setPath("/views");

我希望这对你有所帮助。

于 2013-08-07T23:31:28.443 回答