私は、ASP.NET Webフォームにログインし、一旦認証されると、ファイルをダウンロードする必要のあるJavaプログラムに取り組んでいます。通常のHTTP GET/POSTは問題ではありませんが、Javaから接続するときにASPが私にセッションIDを与えていないようですが、ブラウザからのものです。ASP.NET WebフォームにログインするJavaメソッド
Firefoxでヘッダー情報を見ると、最初のログインからCookieが設定されているように見えますが、ページはすぐに新しいURLにリダイレクトされます。それが重要かどうかはわかりませんが、ログイン後にリダイレクトするページにはiframeが含まれています。メインページとiframe srcの両方を読み込みしようとしましたが、どちらもヘッダーにCookieを渡していません。
//Pull up the login page, extract out the hidden input variables __VIEWSTATE, __EVENTVALIDATION
URL url = new URL(loginPage);
HttpURLConnection conn = null;
conn = (HttpURLConnection) url.openConnection();
//This reads the page line-by-line and extracts out all the values from hidden input fields
Map<String,String> formFields = getViewstate(conn);
//Now re-open the URL to actually submit the POST data
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
DataOutputStream out = new DataOutputStream(conn.getOutputStream());
String postValues = URLEncoder.encode("txtUsername", "UTF-8") + "=" + URLEncoder.encode(uid, "UTF-8");
postValues += "&" + URLEncoder.encode("txtPassword", "UTF-8") + "=" + URLEncoder.encode(pwd, "UTF-8");
postValues += "&" + URLEncoder.encode("__EVENTTARGET", "UTF-8") + "=" + URLEncoder.encode("", "UTF-8");
postValues += "&" + URLEncoder.encode("__VIEWSTATE", "UTF-8") + "=" + URLEncoder.encode(formFields.get("viewstate"), "UTF-8");
postValues += "&" + URLEncoder.encode("__EVENTVALIDATION", "UTF-8") + "=" + URLEncoder.encode(formFields.get("eventvalidation"), "UTF-8");
out.writeBytes(postValues);
out.flush();
out.close();
//At this point looking at Firefox sniffer data, it should be sending back the cookie
//However there is no Set-Cookie in the header fields
for (int i = 1; (key = conn.getHeaderFieldKey(i)) != null; i++) {
// get ASP.NET_SessionId from cookie
if (key.equalsIgnoreCase("set-cookie")) {
sessionId = conn.getHeaderField(key);
sessionId = sessionId.substring(0, sessionId.indexOf(";"));
}
}
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while ((line = rd.readLine()) != null) {
//The page it prints out is the page it was redirected to when logged in through the browser
System.out.println(line);
}
rd.close();
//At this point, it was a successful login, but I never got the cookie so I'm stuck
私はヘッダーを解析してクッキーを抽出することができます。返されるすべての標準ヘッダーを取得しますが、Cookieは取得しません。多分私はクッキーを受け入れるサーバーに明示的に言わなければならない場合を除き、 –
最後のことは良い点です。ほとんどのサーバーはクッキーがサポートされているかどうかをテストするためのメカニズムを持っていると思います。 Weblogicは最初のリクエストでURLとクッキーの両方にセッションIDを設定し、次のリクエストでURL値と予想されるクッキーを比較し、一致すればURLエンコードされたIDを削除します。あなたがクッキーを取得している場合は、それを返送していますか?あなたは本当にリダイレクトを設定していますか?より高いレベルのライブラリはおそらく、カスタムコードのトンなしであなたが望むことを行うための最良の賭けです。 – mezmo