【问题标题】:Android HttpUrlConnection setting POST methodAndroid HttpUrlConnection 设置 POST 方法
【发布时间】:2016-02-08 19:23:09
【问题描述】:

我需要 HttpUrlConnection 来使用 POST 方法,但它的播放效果不好。

connection = (HttpURLConnection) link.openConnection();
connection.setRequestMethod("POST");
...

在调试器中:

  • 在第一行我看到 connection = null 符合预期
  • 第二行我可以看到connection.method = "GET" 符合预期
  • 进一步,我看到 connection.method = "GET" 这不是预期的

如何让 HttpUrlConnection 使用 POST

解决方案

如果您在“HTTPS”协议上打开HttpUrlconnection,该连接将永久覆盖到“GET”的连接。

如果手动设置,通过“HTTP”连接将允许“POST”方法。

【问题讨论】:

  • 如果你使用HTTPS,你应该使用HttpsURLConnection:developer.android.com/reference/javax/net/ssl/…
  • 在整个应用程序中建立了多个连接,一些 http 一些 https,因此为什么要追查这个 bug 有点乱。
  • 啊,我明白了。很高兴你把它整理好了!

标签: android http-post httpurlconnection


【解决方案1】:

解决方案

如果您在“HTTPS”协议上打开HttpUrlconnection,该连接将永久覆盖到“GET”的连接。

如果手动设置,通过“HTTP”连接将允许“POST”方法。

【讨论】:

    【解决方案2】:

    试试看:

    public static String executePostHttpRequest(final String path, Map<String, String> params) throws ClientProtocolException, IOException {
        String result = null;
        HttpURLConnection urlConnection = null;
        try {
            String postData = getQuery(params);
            byte[] postDataBytes = postData.getBytes("UTF-8");
    
            URL url = new URL(path);
            urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setConnectTimeout(30000);
            urlConnection.setReadTimeout(30000);
            urlConnection.setRequestMethod("POST");
            urlConnection.setDoOutput(true);
            urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
            urlConnection.setRequestProperty("charset", "UTF-8");
            urlConnection.setRequestProperty("Content-Length", Integer.toString(postDataBytes.length));
    
            OutputStream out = urlConnection.getOutputStream();
            out.write(postDataBytes);
            out.close();
            result = readStream(urlConnection.getInputStream());
        } finally {
            if (urlConnection != null) {
                urlConnection.disconnect();
            }
        }
        return result;
    }
    

    地点:

    private static String getQuery(Map<String, String> params) throws UnsupportedEncodingException{
        StringBuilder result = new StringBuilder(); 
        boolean haveData = params != null && params.size() > 0;
        if (haveData) {
            boolean first = true;
            for (Map.Entry<String, String> entry : params.entrySet()){
                String value = entry.getValue();
                if (value != null) {
                    if (first) {
                         first = false;
                    } else {
                         result.append("&");
                    }
                    result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
                    result.append("=");
                    result.append(URLEncoder.encode(value, "UTF-8"));
                }
            }
        }
        return result.toString();
    }
    

    附:为了更好地理解,我没有将硬编码的字符串移动到常量中。

    【讨论】:

      【解决方案3】:
      public void postData() {
      // Create a new HttpClient and Post Header
      HttpClient httpclient = new DefaultHttpClient();
      HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php");
      
      try {
          // Add your data
          List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
          nameValuePairs.add(new BasicNameValuePair("id", "12345"));
          nameValuePairs.add(new BasicNameValuePair("stringdata", "Hi"));
          httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
      
          // Execute HTTP Post Request
          HttpResponse response = httpclient.execute(httppost);
      
      } catch (ClientProtocolException e) {
          // TODO Auto-generated catch block
      } catch (IOException e) {
          // TODO Auto-generated catch block
      }
      

      }

      【讨论】:

      • HttpClient 曾经是这个应用程序中的内容,但由于 TLS 1.2 的复杂性,所有内容都已被移出,因此我使用 HttpUrlConnection
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-12
      • 2023-03-12
      相关资源
      最近更新 更多