我创建了通过POST请求登录Foursquare.com的方法。但是我在这条线上有很大的延迟:
conn.getResponseCode();
我需要等待一分钟才能登录。为什么会这样?
也许有办法快速发送POST请求?
代码:
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();
}
答案 0 :(得分:0)
解决方法是使用Jsoup库来发布post \ get请求。
当我使用它时,速度会增加~80倍。