开发者

HttpWebRequest, WebBrowser, and cookies? (visual basic 2010)

I'm writing a program for my sister for a game which she plays online. It uses a WebBrowser control, and HttpWebRequest as well.

The basic function is it searches through the website, we'll say the url is http://hersite.com/pet/* (don't follow the link lol it's just an example) where * is a number and this is how it searches. I use a for loop to cycle the numbers from a specified start to end, and use an httpwebrequest to fetch the html code, then process the string using InStr and .split()

When I access the webpage using the WebRequest.Navigate, it takes me to the page correctly. However, if I fetch the source through the HttpWebRequest, it fetches the source of the login page, which the website redircts you to if you are not logged in.

Is there a way to transfer the cookies from the WebBrowser to my HttpWebRequest?

I hope this is clear.

Some examples:

Get the HTML somewhere in here I need to add the cookie to the http requester, don't I?

Function GetHTML(ByVal strPage As 开发者_StackOverflow中文版String) As String Dim strReply As String = "NULL"

    Try
    Dim objHttpRequest As System.Net.HttpWebRequest
    Dim objHttpResponse As System.Net.HttpWebResponse
    Dim uri As New Uri(strPage)

    objHttpRequest = System.Net.HttpWebRequest.Create(strPage)


    objHttpResponse = objHttpRequest.GetResponse
    Dim objStrmReader As New StreamReader(objHttpResponse.GetResponseStream)

    strReply = objStrmReader.ReadToEnd()

    Catch ex As Exception
    strReply = "ERROR! " + ex.Message.ToString
     End Try

    Return strReply

End Function


what's you question how get or set the cookies in your http request?

Well, if the question is how set,you can make this:

objHttpRequest.Headers["Cookie"] = "baa";

but, if is the latter:

Use tcpdump or Wireshark or some other such tool to capture the network traffic and see what exactly the go code is sending to the server and what the server returns in response. Make http request for login page -> sending login and password(for example) if you get success, the server returned the cookies in response headers. split the label cookie from response and set in your http request as above.


So you're trying to share cookies between the WebBrowser control and an HttpWebRequest object? The former actually use's the system's cookies (Internet Explorer's technically) and as such accessing them could be a security risk. That said, there is a way. Check out this post. It's in C# but you should be able to convert it to VB pretty easily. You'll see one P/Invoke to get access to secure cookies. Ultimately you're just transferring a string from one program to another, you just need to make sure that it gets formatted correctly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜