Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
HTTPS post请求,从HTML到Java_Java_Android_Httpurlconnection_Html Post - Fatal编程技术网

HTTPS post请求,从HTML到Java

HTTPS post请求,从HTML到Java,java,android,httpurlconnection,html-post,Java,Android,Httpurlconnection,Html Post,我想将此HTML请求帖子“翻译”为: 请注意,第一个值是整数而不是字符串 所以我尝试使用stackoverflow上的代码: public String performPostCall(String requestURL, HashMap<String, String> postDataParams) { URL url; String response = ""; try { url = new URL(requestURL);

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


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

所以我尝试使用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();
}
公共字符串performPostCall(字符串请求URL、HashMap postDataParams){
网址;
字符串响应=”;
试一试{
url=新url(请求url);
HttpsURLConnection conn=(HttpsURLConnection)url.openConnection();
连接设置读取超时(15000);
连接设置连接超时(15000);
conn.setRequestMethod(“POST”);
conn.setDoInput(真);
连接设置输出(真);
OutputStream os=conn.getOutputStream();
BufferedWriter=新的BufferedWriter(
新的OutputStreamWriter(操作系统,“UTF-8”);
write(getPostDataString(postDataParams));
writer.flush();
writer.close();
os.close();
int responseCode=conn.getResponseCode();
if(responseCode==HttpsURLConnection.HTTP\u确定){
弦线;
BufferedReader br=新的BufferedReader(新的InputStreamReader(conn.getInputStream());
而((line=br.readLine())!=null){
响应+=行;
}
}
否则{
答复=”;
}
}捕获(例外e){
e、 printStackTrace();
}
返回响应;
}
私有字符串getPostDataString(HashMap参数)引发UnsupportedEncodingException{
StringBuilder结果=新建StringBuilder();
布尔值优先=真;
对于(Map.Entry:params.entrySet()){
如果(第一)
第一个=假;
其他的
结果。追加(&);
append(URLEncoder.encode(entry.getKey(),“UTF-8”);
结果。追加(“=”);
append(URLEncoder.encode(entry.getValue(),“UTF-8”);
}
返回result.toString();
}
在我的任务中,我正在做:

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);
HashMap参数=新的HashMap();
字符串url=”https://mysite.example";
字符串结果;
参数。输入(“键1”、“1”);
参数。输入(“键2”、“2”);
参数。输入(“键3”、“3”);
结果=performPostCall(url、参数);

但这不起作用。怎么了?

我认为您缺少了一条重要的线路,这条线路在输出流之前建立了实际的连接

conn.connect();
您还需要在AndroidManifest.xml中添加权限

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

在“编写器”处理以下内容之前,您正在关闭它:

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

int responseCode=conn.getResponseCode()

你需要嗅探数据包,看看每个平台之间有什么不同,这是反向工程的方法。Robert simply response等于“”…尝试将http响应代码输出到logcat?http响应代码是200。显然我已经使用了权限。我添加了conn.connect(),但仍然不起作用。在代码中,您没有关闭缓冲读取器,没有缓冲读取器,您无法获得响应…因为您已经获得了200,请参考此。。。。我看到的很多投票结果都应该是正确的。Zigma我发现了问题,我需要传递参数JSessionId。你能澄清一下你的答案吗?我看不出这段代码有任何错误,除了调用
BufferedWriter.close()
之前不需要刷新。
writer.flush();
writer.close();
os.close();