Android无法收到表单提交响应

时间:2018-08-04 12:57:50

标签: android sockets post inputstream

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

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

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

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),然后关闭套接字。日志显示实际发送的字节数。

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

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应用程序:

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应用中,收到的发布原始数据如下:

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错误提示意外的字符串结尾

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

1 个答案:

答案 0 :(得分:0)

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

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

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

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

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