简体   繁体   中英

Long POST request to login by HTTPS android

I create method to log in on Foursquare.com through POST request. But I have large delay on this line:

conn.getResponseCode();

I need to wait for minute to log in. Why it happens?

Maybe there are a way to fast POST request?

Code:

public void sendPost(String url, String postParams, String refererLink, String hostLink) throws Exception {
        Log.d(LOG_TAG, "sendPost");
        URL obj = new URL(url);
        conn = (HttpsURLConnection) obj.openConnection();

        // Acts like a browser
        conn.setUseCaches(false);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Host", hostLink);
        conn.setRequestProperty("User-Agent", USER_AGENT);
        conn.setRequestProperty("Accept",
                "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
        conn.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
        this.cookies = new ArrayList<String>();
        for (String cookie : this.cookies) {
            conn.addRequestProperty("Cookie", cookie.split(";", 1)[0]);
        }
        conn.setRequestProperty("Connection", "keep-alive");
        conn.setRequestProperty("Referer", refererLink);
        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        conn.setRequestProperty("Content-Length", Integer.toString(postParams.length()));

        conn.setDoOutput(true);
        conn.setDoInput(true);

        // Send post request
        Log.d(LOG_TAG, "Send post request");
        DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
        wr.writeBytes(postParams);
        wr.flush();
        wr.close();

        conn.getResponseCode();
    }

The solve is to use Jsoup library to make post\\get requests.

When I become to use it the speed grows up in ~80 times.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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