【问题标题】:HTTPS post request, from HTML to JavaHTTPS 发布请求,从 HTML 到 Java
【发布时间】:2015-06-27 00:05:19
【问题描述】:

我想将此 HTML 请求帖子“翻译”为Android

<form name="myForm" action="https://mysite.example" method="POST">
  <input type="hidden" name="Key1" value=1>
  <input type="hidden" name="Key2" value="2">
  <input type="hidden" name="Key3" value="3">
</form>

请注意,第一个值是整数而不是字符串。

所以我尝试使用在 stackoverflow 上找到的代码:

public String  performPostCall(String requestURL, HashMap<String, String> postDataParams) {
    URL url;
    String response = "";
    try {
        url = new URL(requestURL);
        HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
        conn.setReadTimeout(15000);
        conn.setConnectTimeout(15000);
        conn.setRequestMethod("POST");
        conn.setDoInput(true);
        conn.setDoOutput(true);

        OutputStream os = conn.getOutputStream();
        BufferedWriter writer = new BufferedWriter(
                new OutputStreamWriter(os, "UTF-8"));
        writer.write(getPostDataString(postDataParams));

        writer.flush();
        writer.close();
        os.close();
        int responseCode=conn.getResponseCode();

        if (responseCode == HttpsURLConnection.HTTP_OK) {
            String line;
            BufferedReader br=new BufferedReader(new InputStreamReader(conn.getInputStream()));
            while ((line=br.readLine()) != null) {
                response+=line;
            }
        }
        else {
            response="";
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return response;
}

private String getPostDataString(HashMap<String, String> params) throws UnsupportedEncodingException {
    StringBuilder result = new StringBuilder();
    boolean first = true;
    for(Map.Entry<String, String> entry : params.entrySet()){
        if (first)
            first = false;
        else
            result.append("&");

        result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
        result.append("=");
        result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
    }

    return result.toString();
}

在我正在做的 AsyncTask 中:

HashMap<String, String> parameters = new HashMap<>();
String url = "https://mysite.example";
String result;

parameters.put("Key1", "1");
parameters.put("Key2", "2");
parameters.put("Key3", "3");
result = performPostCall(url, parameters);

但这不起作用。怎么了?

【问题讨论】:

  • 你需要嗅探数据包,看看每个平台之间有什么不同,这是逆向工程的方法。
  • 罗伯特简单地响应等于“”..
  • 尝试将http响应码输出到logcat?
  • Http响应码为200。

标签: java android httpurlconnection html-post


【解决方案1】:

我认为您错过了在输出流之前建立实际连接的重要行

conn.connect();

您还应该在 AndroidManifest.xml 中添加权限

<uses-permission android:name="android.permission.INTERNET"/> 

【讨论】:

  • 显然我已经使用了权限。我添加了 conn.connect() 但仍然不起作用。
  • 在代码中,您没有关闭缓冲阅读器,否则您将无法获得响应.....因为您已经获得 200
  • 请参考这个stackoverflow.com/questions/9767952/… .... 我看到的很多赞成票应该有效
  • Zigma 发现问题了,需要传参数jsessionid
【解决方案2】:

您正在关闭“编写器”,然后它才处理:

writer.flush();
writer.close();
os.close();

int responseCode=conn.getResponseCode();

【讨论】:

  • 您能澄清一下您的答案吗?我看不出这段代码有什么问题,只是在调用BufferedWriter.close()之前不需要刷新。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-12-04
  • 1970-01-01
  • 1970-01-01
  • 2020-10-06
  • 1970-01-01
  • 1970-01-01
  • 2016-08-31
相关资源
最近更新 更多