java如何向api发出POST请求
我使用以下代码连接到.Net Web API
,但据我所知,我使用的是POST
方法,因为我使用的是HttpPost
对象,但api说:
The requested resource does not support http method 'GET'.
我的代码:
private boolean POST(List<NameValuePair>[] nvPair) {
HttpClient httpclient = new DefaultHttpClient();
String UrlString = URLEncodedUtils.format(nvPair[0], "utf-8");
HttpPost httppost = new HttpPost(apiBaseUri + UrlString);
try {
httppost.setEntity(new UrlEncodedFormEntity(nvPair[0]));
HttpResponse response = httpclient.execute(httppost);
String respond = response.getStatusLine().getReasonPhrase();
Log.d("MSG 3 > ", respond);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
return true;
}
# 1 楼答案
用于发送HTTP Post请求