Android HTTPS页面上的POST会在回答时返回空白页面

时间:2015-07-05 07:37:26

标签: android post ssl https httpurlconnection

我正在尝试对HTTPS网址发出POST请求。代码如下。问题是

br.readline();

什么都不回报。没有任何类型的任何偏见。

我尝试了其他网址,一些在php中手工制作,在本地服务器上运行并报告回来,我尝试删除

br.flush();
br.close();

但我无法使其发挥作用。 我在这里阅读并关注了很多关于stackoverflow的例子并在互联网上找到了,但我真的无法理解我做错了什么。

SSL部分似乎没问题(在我遇到的其他情况下没有抛出异常)。

我试过没有SSL,结果相同。几乎相同的代码工作,我必须改变最坏的东西。测试平台是一款4.2.2 Android平板电脑。

清单中的权限如下,其中一些用于其他目的:

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

我试过这个:https post in android 4.0但是没办法。我阅读了很多其他文章,但我的想法用完了。

代码:

package shub.wisprv;

import java.io.IOException;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.InputStreamReader;
import android.os.AsyncTask;


public class example extends AsyncTask<Void, Void, String> {

    public void example() {}

    protected String doInBackground(Void... stringarray)  {


        try {
            URL login = new URL("https://it.portal.vodafone-wifi.com/jcp/it?res=login&uamip=192.168.6.1&uamport=80&challenge=f5aa36b054101c5edd56afcd00a10774&mac=18-FE-34-58-B8-80&ip=192.168.6.2&nasid=90-35-6E-41-64-B0&userurl=http://www.amiga.com");

            HttpsURLConnection postConn = (HttpsURLConnection) login.openConnection();

            String postFields = "UserName=foobar@foobar.com&Password=password&chooseCountry=VF_IT/";

            byte[] postData = postFields.getBytes("UTF-8");
            int postDataLength = postData.length;

            postConn.setRequestMethod("POST");
            postConn.setInstanceFollowRedirects(false);
            postConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            postConn.setRequestProperty("charset", "utf-8");
            postConn.setDoOutput(true);
            postConn.setDoInput(true);
            //postConn.setChunkedStreamingMode(0);
            postConn.setRequestProperty("Content-Length", Integer.toString(postDataLength));
            postConn.setUseCaches(false);

            OutputStream wr = (OutputStream) postConn.getOutputStream();
            wr.write(postData);

            wr.flush();
            wr.close();



            String html = "";
            String inputLine;
            BufferedReader br = new BufferedReader(new InputStreamReader(postConn.getInputStream()));
            while ((inputLine = br.readLine()) != null) {
                html += inputLine;
            }

            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return "";

    }
}

使用以下方式调用:

new example().execute();

1 个答案:

答案 0 :(得分:1)

您还必须输入

postConn.connect();

设置完所有参数后,您的代码将如下所示:

public class example extends AsyncTask<Void, Void, String> {

public void example() {}

protected String doInBackground(Void... stringarray)  {


    try {
        URL login = new URL("https://it.portal.vodafone-wifi.com/jcp/it?res=login&uamip=192.168.6.1&uamport=80&challenge=f5aa36b054101c5edd56afcd00a10774&mac=18-FE-34-58-B8-80&ip=192.168.6.2&nasid=90-35-6E-41-64-B0&userurl=http://www.amiga.com");

        HttpsURLConnection postConn = (HttpsURLConnection) login.openConnection();

        String postFields = "UserName=foobar@foobar.com&Password=password&chooseCountry=VF_IT/";

        byte[] postData = postFields.getBytes("UTF-8");
        int postDataLength = postData.length;

        postConn.setRequestMethod("POST");
        postConn.setInstanceFollowRedirects(false);
        postConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        postConn.setRequestProperty("charset", "utf-8");
        postConn.setDoOutput(true);
        postConn.setDoInput(true);
        //postConn.setChunkedStreamingMode(0);
        postConn.setRequestProperty("Content-Length", Integer.toString(postDataLength));
        postConn.setUseCaches(false);
        postConn.connect(); //This is necessary to setup a connection 

        OutputStream wr = (OutputStream) postConn.getOutputStream();
        wr.write(postData);

        wr.flush();
        wr.close();



        String html = "";
        String inputLine;
        BufferedReader br = new BufferedReader(new InputStreamReader(postConn.getInputStream()));
        while ((inputLine = br.readLine()) != null) {
            html += inputLine;
        }

        br.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return "";

}
}