Android上的Http Status 400但桌面上的200

时间:2014-04-24 18:06:38

标签: java android https http-post httpsurlconnection

我正尝试使用以下代码登录Twitter:

//private final String USER_AGENT = "Mozilla/5.0";

private final String USER_AGENT = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36";
public static void main(String[] args) throws Exception {
runThisClass("This is a samplest tweet234!");
}

public static void runThisClass(String tweet)throws Exception{
    String url = "https://mobile.twitter.com/session/new";
    String url2 = "https://mobile.twitter.com/session";
    String url3 = "https://mobile.twitter.com";
    String gmail = "https://mobile.twitter.com/compose/tweet";
HttpUrlConnectionExample http = new HttpUrlConnectionExample();

// make sure cookies is turn on
CookieHandler.setDefault(new CookieManager());

// 1. Send a "GET" request, so that you can extract the form's data.
String page = http.GetPageContent(url);
String postParams = http.getFormParams(page, "username", "password");

// 2. Construct above post's content and then send a POST request for
// authentication
http.sendPost(url2, postParams);

// 3. success then go to compose tweet page.
String result = http.GetPageContent(gmail);

//4.Construct another HTTP Post to tweet:
postParams=http.getTweetFormParams(result,tweet);
http.sendPostTweet(url3, postParams);
}

private void sendPost(String url, String postParams) throws Exception {
URL obj = new URL(url);
conn = (HttpsURLConnection) obj.openConnection();

// Acts like a browser
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Host", "twitter.com");
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");
for (String cookie : this.cookies) {
    conn.addRequestProperty("Cookie", cookie.split(";", 1)[0]);
}
conn.setRequestProperty("Connection", "keep-alive");
conn.setRequestProperty("referer", "https://mobile.twitter.com/session");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", Integer.toString(postParams.length()));

conn.setDoOutput(true);
conn.setDoInput(true);
System.out.println("SendPostPost Params: "+postParams);
// Send post request
DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
wr.writeBytes(postParams);
wr.flush();
wr.close();

int responseCode = conn.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + postParams);
System.out.println("Response Code : " + responseCode);

BufferedReader in =
        new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();

while ((inputLine = in.readLine()) != null) {
    response.append(inputLine);
}
in.close();
System.out.println(response.toString());

}

现在我面临的问题是(在方法sendPost()中):

int responseCode = conn.getResponseCode();

在普通的PC上,这很好用,我得到的响应代码为200.但是当在Android上运行时,这给了我400.我想知道这里有什么不对。非常感谢和要求任何帮助。

0 个答案:

没有答案