2016-09-14 6 views
0

私は、Webサイトの特定のページのHTMLを取得して返すWebサービスを作成しています。私は最初にログイン情報をログインページにPOSTしようとしているので、私が必要とするページにアクセスするために必要なクッキーを手に入れます。それから、私は実際に欲しいページを手に入れようとしています。VB.NET - POST要求が正しく実行されていません

はここに私のコードです:

Dim http As HttpWebRequest = TryCast(WebRequest.Create("http://www.mywebsite.com/loginpage"), HttpWebRequest) 
http.KeepAlive = True 
http.Method = "POST" 
http.ContentType = "application/x-www-form-urlencoded" 
Dim postData As String = "My post data" 
Dim dataBytes As Byte() = UTF8Encoding.UTF8.GetBytes(postData) 
http.ContentLength = dataBytes.Length 

Using postStream As Stream = http.GetRequestStream() 
    postStream.Write(dataBytes, 0, dataBytes.Length) 
End Using 

Dim httpResponse As HttpWebResponse = TryCast(http.GetResponse(), HttpWebResponse) 
http = TryCast(WebRequest.Create("http://www.mywebsite.com/desiredpage"), HttpWebRequest) 
http.CookieContainer = New CookieContainer() 
http.CookieContainer.Add(httpResponse.Cookies) 
Dim httpResponse2 As HttpWebResponse = TryCast(http.GetResponse(), HttpWebResponse) 

Using httpResponse2 
    Using reader As New StreamReader(httpResponse2.GetResponseStream()) 
     Dim html As String = reader.ReadToEnd() 
     Return html 
    End Using 
End Using 

私の問題はこれです:代わりに正常にログインし、必要なクッキーを返すので、私は戻って、ログインページのHTMLを受けます。 mywebsite.com/desiredpageは、クッキーが存在しない場合はログインページにリダイレクトされるので、これは全く驚くことではありません。

UPDATE:.ASPXANONYMOUSlanguageUSERNAME_CHANGEDauthentication.DOTNETNUKE、およびreturnurl Wiresharkのは、ウェブサイトが6枚のクッキーを返していることを私に語っています。私は最初の3がhttp.CookieContainerに格納されているが、残りの3つは格納されていないことを確認しました。

残りの3には何が起こっていますか?

答えて

0

私の問題は、私は2番目に最初の要求からクッキーを渡した方法でした。

Dim http As HttpWebRequest = TryCast(WebRequest.Create("http://www.mywebsite.com/loginpage"), HttpWebRequest) 
http.KeepAlive = True 
http.Method = "POST" 
http.ContentType = "application/x-www-form-urlencoded" 
Dim postData As String = "My post data" 
Dim dataBytes As Byte() = UTF8Encoding.UTF8.GetBytes(postData) 
http.ContentLength = dataBytes.Length 
Dim myCookies As CookieContainer = New CookieContainer() 
http.CookieContainer = myCookies 

Using postStream As Stream = http.GetRequestStream() 
    postStream.Write(dataBytes, 0, dataBytes.Length) 
End Using 

Dim httpResponse As HttpWebResponse = TryCast(http.GetResponse(), HttpWebResponse) 
Dim http2 As HttpWebRequest = TryCast(WebRequest.Create("http://www.mywebsite.com/desiredpage"), HttpWebRequest) 
http2.CookieContainer = myCookies 
Dim httpResponse2 As HttpWebResponse = TryCast(http2.GetResponse(), HttpWebResponse) 

Using httpResponse2 
    Using reader As New StreamReader(httpResponse2.GetResponseStream()) 
     Dim html As String = reader.ReadToEnd() 
     Return html 
    End Using 
End Using 

別々CookieContainerの作成2つの別々のリクエストでそれを使用するには、問題を修正しました

関連する問題