繁体   English   中英

HttpURLConnection发布Android

[英]HttpURLConnection Post Android

我试图使用教程这里用我学到的数据在这里进行HttpURLConnection的POST请求。 每次我运行它时,响应都会显示错误消息:“您必须输入用户名!” 和“您必须输入密码”。 谁能解释一下为什么服务器无法识别甚至无法获取发布的数据? 这是我的代码:

class urlRequest extends AsyncTask<String, String, String> {

protected String doInBackground(String... all) {
            URL url;
            String response = "";
            try {
                String requestURL = "https://hac.chicousd.org/LoginParent.aspx?page=GradebookSummary.aspx";
                url = new URL(requestURL);

                HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
                conn.setReadTimeout(15000);
                conn.setConnectTimeout(15000);
                conn.setRequestMethod("POST");
                conn.setDoInput(true);
                conn.setDoOutput(true);
                conn.setRequestProperty( "Content-Type", "application/x-www-form-urlencoded");
                conn.setRequestProperty( "charset", "utf-8");
                conn.setRequestProperty( "Content-Length", Integer.toString(getPostDataString(postDataParams).length()));
                conn.setFixedLengthStreamingMode(getPostDataString(postDataParams).length());


                JSONObject postDataParams = new JSONObject();
                postDataParams.put("checkCookiesEnabled", "true");
                postDataParams.put("checkMobileDevice", "false");
                postDataParams.put("checkStandaloneMode", "false");
                postDataParams.put("checkTabletDevice", "false");
                postDataParams.put("portalAccountUsername", "username");
                postDataParams.put("portalAccountPassword", "password");

                DataOutputStream os = new DataOutputStream(conn.getOutputStream());
                BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));

                writer.write(getPostDataString(postDataParams)); // returns: checkCookiesEnabled=true&checkMobileDevice=false&checkStandaloneMode=false&checkTabletDevice=false&portalAccountUsername=username&portalAccountPassword=password

                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;
                        Log.i("tag", line);
                    }
                }
                else {
                    response="";
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
    return response;
           }

public String getPostDataString(JSONObject params) throws Exception {

    StringBuilder result = new StringBuilder();
    boolean first = true;

    Iterator<String> itr = params.keys();

    while(itr.hasNext()){

        String key= itr.next();
        Object value = params.get(key);

        if (first)
            first = false;
        else
            result.append("&");

        result.append(URLEncoder.encode(key, "UTF-8"));
        result.append("=");
        result.append(URLEncoder.encode(value.toString(), "UTF-8"));

    }
    return result.toString();
}

使用POST发布参数:

URL url;
URLConnection urlConn;
DataOutputStream printout;
DataInputStream  input;
url = new URL (getCodeBase().toString() + "env.tcgi");
urlConn = url.openConnection();
urlConn.setDoInput (true);
urlConn.setDoOutput (true);
urlConn.setUseCaches (false);
urlConn.setRequestProperty("Content-Type","application/json");   
urlConn.setRequestProperty("Host", "android.schoolportal.gr");
urlConn.connect();  
//Create JSONObject here
JSONObject jsonParam = new JSONObject();
jsonParam.put("portalAccountUsername", "username");
jsonParam.put("portalAccountPassword", "password");

您错过的部分在以下内容中,即如下。

// Send POST output.
printout = new DataOutputStream(urlConn.getOutputStream ());
printout.writeBytes(URLEncoder.encode(jsonParam.toString(),"UTF-8"));
printout.flush ();
printout.close ();

您会收到来自验证的消息,因此请在使用前检查用户名密码(不为空或空白)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM