我如何弄乱我的HttpGet参数编码?

时间:2012-11-07 14:31:55

标签: java uri http-get apache-httpclient-4.x

我有这个URL(在浏览器中执行正常):

https://api-test.company.com/contacts/?q=email=jlowder@company.com

我正在尝试使用HttpClient来执行。我试过了:

    String URL = "https://api-test.company.com/contacts?q=" + URLEncoder.encode("email=jlowder@company.com");
    DefaultHttpClient client = new DefaultHttpClient();
    HttpGet c = new HttpGet(URL);
    HttpResponse response = client.execute(c);
    String resultContent = myRestCall.GetResponseText(response);

这将返回错误:

QUERY_PARAMunable解析查询字符串'email=jlowder@company.com'

我试过了:

    GetMethod method = new GetMethod("https://api-test.company.com/contacts/?q=");
    method.setQueryString(new NameValuePair[] {new NameValuePair("email", "jlowder@company.com")});
    HttpGet c = new HttpGet(method.toString());
    DefaultHttpClient client = new DefaultHttpClient();
    HttpResponse response = client.execute(c);

返回:

java.lang.IllegalStateException:目标主机不能为空,或者在参数中设置。

那么我究竟如何正确解析这个简单的GET请求呢?

感谢。

1 个答案:

答案 0 :(得分:0)

在您的示例中,您仍然有一个参数q也应该传递。 Normaly,你有关键/价值观。为什么你有这样一个星座?

?q=email=jlowder@company.com

缺少“q”值。或者你想将“email=jlowder@company.com”设置为“q”的值然后你应该逃避“=”