我有这个 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_PARAM 无法解析查询字符串“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 请求呢?
谢谢。