Android无法收到表单提交响应 [英] Android could not receive form submit response

查看:106
本文介绍了Android无法收到表单提交响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个python网络服务器,它正在侦听连接,并根据请求对其进行响应.感兴趣的python服务器上的代码部分是POST请求( http://192.168.0.1/conf_wifi_app.html).它需要4个参数

I have a python webserver, which is listening for connections, and responds to them based on the request. The portion of code on python server of interest is POST request (http://192.168.0.1/conf_wifi_app.html). It takes 4 arguments

  1. 用户名(管理员)
  2. 密码(管理员)
  3. essid(家庭无线网络SSID)
  4. 密码(家庭wifi网络密码)

在python服务器上,验证后正文参数后,将像这样发送响应(注意,我已将日志进行调试):

On python server, after the post body parameters are validated, a response is to be sent like so (notice I've put logs for debugging):

json_response = '{"error":false,' + '"code":"' + str(activation_code) + '","mac":"' + str(macaddress) + '","message":"Device configured successfully"}'
bytes_sent = client_s.send(json_response)
client_s.close()
print("Bytes sent " + str(bytes_sent))
print("json_response : " + json_response)

其中client_s是客户端套接字.套接字上的发送"功能应发送响应(json_response),然后关闭套接字.日志显示实际发送的字节数.

where client_s is the client socket. "send" function on socket should send the response (json_response), and then we close the socket. Logs print the number of bytes which is actually sent.

从网络浏览器或邮递员插件发出POST请求时,客户端响应良好.仅供参考,我在chrome浏览器中从邮递员插件调用时提出了原始请求:

The client responds perfectly well when POST request is done from the web browser or from postman plugin. Just for some reference, I've put the raw request when invoked from postman plugin on chrome browser:

POST /conf_wifi_app.html HTTP/1.1
Host: 192.168.0.1
Connection: keep-alive
Content-Length: 67
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/68.0.3440.75 Chrome/68.0.3440.75 Safari/537.36
Cache-Control: no-cache
Origin: chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop
Postman-Token: 4f4a14a7-857d-666f-a2db-279731c83b4a
Content-Type: application/x-www-form-urlencoded
Accept: /
Accept-Encoding: gzip, deflate
Accept-Language: en-GB,en-US;q=0.9,en;q=0.8

essid=NETGEAR-XXXX&passphrase=XXXXXXXX&username=admin&password=admin&submit=submit

从网络浏览器(或邮递员)发出POST请求时,将收到响应.现在,我试图创建一个执行以下POST请求的android应用程序:

The response is received when the POST request is made from web browser (or postman). Now I was trying to create an android app which does the same POST request as follows:

HttpURLConnection urlConnection = null;

try {

    Map<String,Object> params = new LinkedHashMap<>();
    params.put("user", Constants.DEVICE_DEFAULT_USER);
    params.put("username", Constants.DEVICE_DEFAULT_USER);
    params.put("password", Constants.DEVICE_DEFAULT_PASSWORD);
    params.put("essid", homeWifiSSID.replaceAll("^\"|\"$", ""));
    params.put("passphrase", homeWifiPassword);

    StringBuilder urlParameters = new StringBuilder();
    for (Map.Entry<String,Object> param : params.entrySet()) {
        if (urlParameters.length() != 0) urlParameters.append('&');
        urlParameters.append(URLEncoder.encode(param.getKey(), "UTF-8"));
        urlParameters.append('=');
        urlParameters.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
    }
    byte[] postData = urlParameters.toString().getBytes("UTF-8");
    int    postDataLength = postData.length;

    URL url = new URL(Constants.DEVICE_CONFIG_URL);
    urlConnection = (HttpURLConnection) network.openConnection(url);

    urlConnection.setDoInput( true );
    urlConnection.setDoOutput( true );
    urlConnection.setInstanceFollowRedirects( false );
    urlConnection.setRequestMethod( "POST" );
    urlConnection.setRequestProperty( "Content-Type", "application/x-www-form-urlencoded");
    urlConnection.setRequestProperty( "charset", "utf-8");
    urlConnection.setRequestProperty( "Content-Length", Integer.toString( postDataLength ));
    urlConnection.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,/;q=0.8");
    urlConnection.setRequestProperty("Accept-Encoding", "gzip, deflate");
    urlConnection.setUseCaches( false );

    urlConnection.setConnectTimeout(OuroborosAPI.CONNECTION_TIMEOUT);
    urlConnection.setReadTimeout(OuroborosAPI.CONNECTION_TIMEOUT);

    try( DataOutputStream wr = new DataOutputStream( urlConnection.getOutputStream())) {
        wr.write( postData );
        wr.flush();
        wr.close();
    }

    Reader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "UTF-8"));
    for (int c; (c = in.read()) >= 0;) {
        System.out.print((char) c);
    }
}
catch (Exception e) {
}

从android应用中,收到的发布原始数据如下:

From android app, the post raw data received is as follows:

POST /conf_wifi.html HTTP/1.1
Content-Type: application/x-www-form-urlencoded
charset: utf-8
Content-Length: 85
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,/;q=0.8
User-Agent: Dalvik/2.1.0 (Linux; U; Android 7.1.1; Moto G (5S) Plus Build/NPSS26.116-61-11)
Host: 192.168.0.1
Connection: Keep-Alive
Accept-Encoding: gzip

essid=NETGEAR-XXXX&passphrase=XXXXXXXX&username=admin&password=admin&submit=submit

在这种情况下,python套接字确实发送了数据(从日志中确认),但是android错误提示意外的字符串结尾.

The python socket in this case does send data (as confirmed from the logs), but the android errors out saying unexpected end of string.

我确实尝试了所有事情(例如添加额外的标题等),但无济于事.请帮助或建议我可能要去哪里了.

I've literally tried every thing (like adding extra headers, etc) but to no avail. Please help or suggest where I may be going wrong.

推荐答案

问题不是我没有发送\ n结束行,或者没有使用readline()

The problem was not that I was not sending \n ended lines, or not using readline()

我没有像这样发送HTML原始标头

I was not sending the HTML raw headers like so

HTTP/1.1 200确定 内容长度:9328 内容类型:text/html

HTTP/1.1 200 OK Content-Length: 9328 Content-Type: text/html

...实际内容/有效载荷......

... Actual content/payload....

一旦我也发送了标头,代码就可以正常工作了.

Once I sent the headers also, the code worked without any problems.

这篇关于Android无法收到表单提交响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆