【问题标题】:HttpPost - type of http request methodHttpPost - http 请求方法的类型
【发布时间】:2012-06-04 13:26:13
【问题描述】:

嗨 :) 我遇到了一个简单的问题,但很烦人。 我正在尝试使用 HttpPost 类发送 http post 请求

这是返回 InputStream 的方法的一部分:

           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;

但问题出在服务器上,因为服务器“认为”我发送的是 GET 请求而不是 POST。问题是:错误在哪里?我在其他应用程序中使用类似的代码部分,它工作正常。

干杯。 :)

【问题讨论】:

  • 也许您需要将调用更改为 GET。你有什么样的服务?是 php 吗? php 对 post 和 get 进行了明确的区分,如果它期望 GET 上的参数,它将无法从 POST 中获取它。
  • webservice 正在等待 POST 请求,没有机会使用 GET 发送参数 :(
  • 是什么让您认为服务器正在查看 GET 请求?
  • 我可以看到服务器日志。并且服务器的响应是空的,就像请求中没有参数一样。
  • 如果您使用 GET 则还将 url 字符串重写为 url+path+"?action=YOUR_WEB-SERVICE_NAME&parameter1name=value1&parameter2name=value2"

标签: java android http post http-post


【解决方案1】:

请尝试

httpPost.setEntity(new UrlEncodedFormEntity(paramsList, HTTP.UTF_8));

代替

httpPost.setEntity(new UrlEncodedFormEntity(paramsList));

【讨论】:

  • 这就是全部,我在我的方法中得到的,不同之处仅在于参数。我正在使用 BufferedReader 和 readLine() 方法从输入流中读取数据。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-08-23
  • 1970-01-01
  • 1970-01-01
  • 2015-05-17
  • 1970-01-01
  • 2023-03-25
  • 1970-01-01
相关资源
最近更新 更多