Java HttpPost—http请求方法的类型
嗨:)我有一个简单的问题,但很烦人。 我正在尝试使用HttpPost类发送http post请求 这是返回InputStream的方法的一部分:Java HttpPost—http请求方法的类型,java,android,http,post,http-post,Java,Android,Http,Post,Http Post,嗨:)我有一个简单的问题,但很烦人。 我正在尝试使用HttpPost类发送http post请求 这是返回InputStream的方法的一部分: HttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(url+path); ArrayList<NameValuePair> paramsList = new ArrayLis
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url+path);
ArrayList<NameValuePair> paramsList = new ArrayList<NameValuePair>();
paramsList.add(new BasicNameValuePair("key1", value1));
paramsList.add(new BasicNameValuePair("key2", value2));
paramsList.add(new BasicNameValuePair("key3", value3));
httpPost.setEntity(new UrlEncodedFormEntity(paramsList));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
InputStream is = httpEntity.getContent();
return is;
HttpClient-HttpClient=newdefaulthttpclient();
HttpPost HttpPost=新的HttpPost(url+路径);
ArrayList paramsList=新的ArrayList();
添加(新的BasicNameValuePair(“键1”,值1));
添加(新的BasicNameValuePair(“键2”,值2));
添加(新的BasicNameValuePair(“键3”,值3));
setEntity(新的UrlEncodedFormEntity(paramsList));
HttpResponse HttpResponse=httpClient.execute(httpPost);
HttpEntity HttpEntity=httpResponse.getEntity();
InputStream=httpEntity.getContent();
回报是;
但问题出在服务器上,因为服务器“认为”im发送GET请求而不是POST。问题是:错误在哪里?我在其他应用程序中使用了类似的部分代码,效果很好
干杯。:) 请试一试
httpPost.setEntity(new UrlEncodedFormEntity(paramsList, HTTP.UTF_8));
而不是
httpPost.setEntity(new UrlEncodedFormEntity(paramsList));
也许你需要改变你的电话以获得更多。你们有什么服务?是php吗?php明确区分了post和get,如果它在get上需要一个参数,它将无法从post中获取该参数。webservice需要post请求,则无法使用get发送参数:(什么使您认为服务器正在看到GET请求?我可以看到服务器日志。服务器的响应是空的,就像请求中没有参数一样。如果使用GET,则还可以将url字符串重写为url+path+“?操作=您的WEB服务\u名称和参数1Name=值1和参数2Name=值2”这就是我在方法中得到的全部内容,区别仅在于参数。我使用BufferedReader和readLine()方法从输入流中读取。