使用HTTPWebRequest并在cookie中存储登录状态 [英] Using HTTPWebRequest and storing login status in a cookie

查看:76
本文介绍了使用HTTPWebRequest并在cookie中存储登录状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,首先,感谢任何对此有任何帮助的人 - 我意识到我可能犯了一些非常基本的错误,但我的编码是超级生锈的...

总之,什么我想做的是使用HTTPWEBREQUEST登录到返回报表数据的Web服务。 Web服务通过验证包含用户名和密码的HTTP字符串并在传递失败时返回OK,INVALID等来工作。然后,随后的HTTP请求允许您在第一步中基于成功的身份验证返回各种数据集等。

如果我手动将URL一个接一个地传递到浏览器中,这样可以正常工作。但是,当我尝试在代码中执行此操作时(如下所示),第一个HTTP请求返回OK但第二个显示为NOTLOGGEDIN,即它没有存储第一个HTTP请求的状态等。任何有关我的帮助我做得不对,我将不胜感激!

提前致谢! Gar



Hello folks, first up, thanks to anyone with any help on this - I realise I've probably made some really basic error but my coding is uber rusty...

In summary, what I am trying to do is to use the HTTPWEBREQUEST to login into a web service which returns report data. The web service works by validating a HTTP string which contains the username and password and returning OK, INVALID etc on pass fail. The subsequent HTTP request then allow you to return various datasets etc all based on successful authentication in the first step.

This works fine if I manually pass the URLs into a browser, one after the other. However, when I try to do this in code (as below), the first HTTP request returns OK but the second one shows me as NOTLOGGEDIN, i.e. it hasn't stored the status for the first HTTP request etc. Any help on what I'm not doing right would be greatly appreciated!

Thanks in advance! Gar


 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGO.Click

        'Address of URL

        'Dim Cookies As CookieContainer

        Dim URL As String

        Dim request As Net.HttpWebRequest

        Dim response As HttpWebResponse

        Dim reader As StreamReader

        Dim str As String

        Dim sNode As String

        Dim sessioncookie

        

        ' ***** URL BELOW WILL RETURN OK/INVALID BASED ON SUCCESS OR FAILURE

        URL = "https://samplesite.com/logIn.aspx?username=xxxx@yyyyy.com&pwd=aaabbbccc"

        request = WebRequest.Create(URL)

        request.CookieContainer = New CookieContainer()

        response = request.GetResponse()

        reader = New StreamReader(response.GetResponseStream())

        str = reader.ReadLine()



        sessioncookie = request.CookieContainer



        ' ***** URL BELOW SHOULD RETURN REPORT DATASET BASED ON SUCCESS OF URL BELOW 

        ' ***** BUT CURRENTLY IS NOT RECOGNISING THE LOGGED IN STATUS RETURNED ABOVE

        request = WebRequest.Create("https://samplesite.com/API/getReport.aspx?id=3823158")

        request.CookieContainer = sessioncookie

        response = request.GetResponse()

        reader = New StreamReader(response.GetResponseStream())

        str = reader.ReadLine()



        sNode = txtNODE.Text



        Do While str.Length > 0

            'Console.WriteLine(str)

            txtOUTPUT.Text = txtOUTPUT.Text & str

            str = reader.ReadLine()

        Loop



    End Sub

推荐答案



请看看这篇关于的文章在.NET中使用HttpWebRequest和HttpWebResponse。您需要从HttpWebResponse获取cookie并在以后的Web请求中使用cookie。请让我知道结果。
http://www.codeproject.com/KB/IP/httpwebrequest_response .aspx


这篇关于使用HTTPWebRequest并在cookie中存储登录状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆