私はJavaでプログラムを書いていますが、フォームの指定されたフィールドに入力し、送信クリックをシミュレートしたいので、結果ページを取得します。http://stu21.kntu.ac.ir/Login.aspx
2つのフィールドtxtusername
と。を持つURLでアイデアをテストしていますtxtpassword
。次のようにコードを試していますが、結果ページが返されません。どうすればいいですか?このコードの何が間違っていますか?
DefaultHttpClient conn = new DefaultHttpClient();
conn = new DefaultHttpClient();
ArrayList<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("txtusername", "8810103"));
pairs.add(new BasicNameValuePair("txtpassword", "8810103"));
HttpPost httpPost = new HttpPost("http://stu21.kntu.ac.ir/Login.aspx");
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(pairs,
"UTF-8");
httpPost.setHeader(
"UserAgent",
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.19 (KHTML, like Gecko) Ubuntu/12.04 Chromium/18.0.1025.151 Chrome/18.0.1025.151 Safari/535.19");
httpPost.setEntity(entity);
HttpResponse response = conn.execute(httpPost);
InputStream is = response.getEntity().getContent();
RandomAccessFile raf = new RandomAccessFile(
"/home/hossein/Desktop/random.aspx", "rw");
raf.seek(0);
int bytes = 0;
byte[] buffer = new byte[1024];
while ((bytes = is.read(buffer)) != -1)
raf.write(buffer, 0, bytes);
raf.close();
is.close();
質問が別のスレッドと重複している場合は申し訳ありませんが、他のスレッドで解決策を見つけることができません。
前もって感謝します :)