0
我对这个Apache http客户端很新。我有一个网址来对其中一个服务进行web服务调用。我成功地执行GET请求,但我试图用POST请求执行此操作,但我没有得到任何回应。我无法从实体获取内容。Apache http post example
这是使用GET方法执行的。下面你可以看到我的代码。
public class HttpClientPostExample {
public static void main(String[] args) throws ClientProtocolException,
IOException {
String url = "https://maps.googleapis.com/maps/api/place/details/xml?";
HttpClient client = HttpClientBuilder.create().build();
// HttpRequest httpRequest = HttpsClientImpl.createRequest("Post", url);
HttpPost httpPost = new HttpPost(url);
List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>();
nameValuePairList
.add(new BasicNameValuePair(
"reference",
"CoQBcQAAAEZ7yCju-0lhU7sZIBBe_On9jYImWzZ9Zt5rIg1tX6zaH02dHrQMHF1LFHY1_yUuXzsUf6m6-rrQJ8Ec_mGxBYtV85Wyb4anakaUi3QuZj7ygJXB3Fd5x69k_4UnDKMmEBNa410vbCXgQOGIkHCbNpcbC8ENxmVlUrqiifmdfuLgEhCtPATMhFRdsjuyAL_j__OEGhTnqujRRMYy_5-kxzcqCdMY4_1dbA"));
nameValuePairList.add(new BasicNameValuePair("sensor", "true"));
nameValuePairList.add(new BasicNameValuePair("key",
"AIzaSyBA0Hu3is9qIJ5v6NEuofigk0y-aQwqiP0"));
httpPost.addHeader("User-Agent", "User-Agent");
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairList, "UTF-8"));
HttpResponse response = client.execute(httpPost);
System.out.println(response.getStatusLine().getStatusCode());
Header[] headerArray = response.getAllHeaders();
for (Header header : headerArray) {
System.out.println("Header Name: " + header.getName()
+ " Header Value: " + header.getValue());
}
}
任何人都可以帮助我做到这一点。这是做POST请求的正确方法...?
如何在触发/调用执行方法之前获取实际的URL ... ???