Https发布请求在Android中不起作用

时间:2014-07-25 01:51:45

标签: android post login https header

我试图通过发送Https Post Request并在登录“in android”后为下一页创建源代码,将loginApplication打入网站 我看很多视频但没什么用的: 任何帮助请在此代码中:

public class MainActivity extends Activity {
    TextView text;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text = (TextView)findViewById(R.id.textView2);
        try {
            new DownloadSourceCodeTask().execute("Link");
        } catch (Exception e) {
            Toast.makeText(getApplicationContext(), "Error in Main", Toast.LENGTH_LONG)
            .show();
        }

    }   private InputStream OpenHttpConnection(String urls) throws IOException {
        InputStream in = null;
        int response = -1;
        URL url = new URL(urls);
        URLConnection conn = url.openConnection();
        if (!(conn instanceof HttpURLConnection))
            throw new IOException("Not An Http Connction");
        try {
            HttpURLConnection httpConn = (HttpURLConnection) conn;
            String param="ssousername=" + URLEncoder.encode("usernamevalue","UTF-8")+
            "password="+URLEncoder.encode("passwordvalue","UTF-8");
            httpConn.setDoOutput(true);
            httpConn.setAllowUserInteraction(false);
            httpConn.setInstanceFollowRedirects(true);
            httpConn.setRequestMethod("POST");
            httpConn.setFixedLengthStreamingMode(param.getBytes().length);
            httpConn.setRequestProperty("Content-Type", "text/html; charset=WINDOWS-1256");
            httpConn.connect();
            response = httpConn.getResponseCode();
            if (response == HttpURLConnection.HTTP_OK) {
                in = httpConn.getInputStream();
            }
        } catch (Exception e) {
            Log.d("Networking", e.getLocalizedMessage());
        }
        return in;
    }


    private String DownloadSourceCode(String url){
        int BUFFER_SIZE = 2000;
        InputStream in = null;
        try {
            in=OpenHttpConnection(url);
        } catch (IOException e) {
            Log.d("Error in connection",e.getLocalizedMessage());
            return "";
        }

        InputStreamReader isr = new InputStreamReader(in);
        int charRead;
        String str = "";
        char[]inputBuffer = new char[BUFFER_SIZE];

        try{
            while((charRead = isr.read(inputBuffer))>0){
                String readString = String.copyValueOf(inputBuffer,0,charRead);
                str+=readString;
                inputBuffer = new char[BUFFER_SIZE];
            }
            in.close();
        }catch(IOException e){
            Log.d("Error in connection",e.getLocalizedMessage());
            return "";
        }
        return str;
    }


    private class DownloadSourceCodeTask extends AsyncTask<String , Void , String>{

        ProgressDialog dialog;
        protected String doInBackground(String... urls){
            return DownloadSourceCode(urls[0]);
        }

        @Override
        protected void onPreExecute() {
            dialog = ProgressDialog.show(MainActivity.this, "", 
                    "Loading. Please wait...", true);
        }

        protected void onPostExecute(String result){
            if(dialog.isShowing()){
                dialog.cancel();
            }
            text.setText(result);
        }
    }

}

1 个答案:

答案 0 :(得分:0)

尝试添加'&amp;'在password参数之前,在connect()之后添加2行,并在content-type中将编码更改为UTF-8。请参阅以下新代码:

try {
        HttpURLConnection httpConn = (HttpURLConnection) conn;
        String param="ssousername=" + URLEncoder.encode("usernamevalue","UTF-8")+
        "&password="+URLEncoder.encode("passwordvalue","UTF-8");
        httpConn.setDoOutput(true);
        httpConn.setAllowUserInteraction(false);
        httpConn.setInstanceFollowRedirects(true);
        httpConn.setRequestMethod("POST");
        httpConn.setFixedLengthStreamingMode(param.getBytes().length);
        httpConn.setRequestProperty("Content-Type", "text/html; charset=WINDOWS-1256");
        httpConn.connect();
        httpConn.getOutputStream().write(param.getBytes());
        httpConn.getOutputStream().flush();
        response = httpConn.getResponseCode();
        if (response == HttpURLConnection.HTTP_OK) {
            in = httpConn.getInputStream();
        }
    } catch (Exception e) {
        Log.d("Networking", e.getLocalizedMessage());
    }

希望它有所帮助。