2012-11-07 26 views
0

我有这个URL(其执行在浏览器罚款):我如何搞乱我的HttpGet参数编码?

https://api-test.company.com/contacts/[email protected]

,我想使用HttpClient的执行。我已经试过:

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

这将返回错误:

QUERY_PARAMunable分析查询字符串 '[email protected]'

我已经试过:

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

返回:

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

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

谢谢。

+0

正确地说谁是正确的。 –

+0

我不确定我明白你在问什么。我问如何正确执行GET请求与给定的参数。 – Jason

回答

0

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

[email protected]

没有为 “Q” 缺少值。或者你是否想将“[email protected]”设置为“q”的值,那么你应该避开“=”

+0

这似乎是问题所在。我需要在参数中包含q =。当然,返回值现在与我在网页中执行时得到的值不同,但那是另一回事。 – Jason