以下のコードを使用して 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 になります。どんな助けも非常に高く評価され、必要とされています。