HttpWebRequest并发布登录数据 [英] HttpWebRequest and posting login data

查看:95
本文介绍了HttpWebRequest并发布登录数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的ASP.NET程序员,


如何将数据发布到ASP.NET登录页面并通过身份验证?

登录页面使用表格

认证,用户必须提供用户名和密码,并且必须在提交按钮上点击

。我想要通过提供HttpWebRequest和

的值来自动化这个过程,然后在网站上下载一个文件。
。我认为我无法调用提交按钮。 Pleeeasee帮助,

提前感谢


Dim myWebReq As HttpWebRequest

Dim myWebResp As HttpWebResponse

Dim编码As New System.Text.ASCIIEncoding()

Dim postData As String

Dim data()As Byte

Dim sr As StreamReader

Dim sw As StreamWriter


postData + =" txtUsername = b"

postData + ="&"

postData + =" txtPassword = k"

data = encoding.GetBytes(postData)

myWebReq =

WebRequest.Create(" http://burak/database/medicalDocs/F325_fittoflyreport.asp

x")

myWebReq.Method =" POST"

myWebReq.ContentType =" application / x-www-form-urlencoded"

myWebReq.ContentLength = data.Length

Dim myStream As Stream = myWebReq。 GetRequestStream()

myStream.Write(data,0,data.Length)

myStream.Close()

myWebResp = myWebRe q.GetResponse

sr =新的StreamReader(myWebResp.GetResponseStream)

Dim strHTML As String = sr.ReadToEnd

sw = File.CreateText( " d:\Downloads\1.htm")

sw.WriteLine(strHTML)

sw.Close()

回复.WriteFile(" d:\Downloads\1.htm")

Dear ASP.NET Programmers,

How can I post data to an ASP.NET login page and pass authentication? The
login page uses forms
authentication, users must supply usernames and password and have to click
on a submit button. I
want to automate this process by supplying values with HttpWebRequest and
then download a file on
the site. I think that I cannot invoke the submit button. Pleeeasee help,
thanks in advance

Dim myWebReq As HttpWebRequest
Dim myWebResp As HttpWebResponse
Dim encoding As New System.Text.ASCIIEncoding()
Dim postData As String
Dim data() As Byte
Dim sr As StreamReader
Dim sw As StreamWriter

postData += "txtUsername=b"
postData += "&"
postData += "txtPassword=k"
data = encoding.GetBytes(postData)
myWebReq =
WebRequest.Create("http://burak/database/medicalDocs/F325_fittoflyreport.asp
x")
myWebReq.Method = "POST"
myWebReq.ContentType = "application/x-www-form-urlencoded"
myWebReq.ContentLength = data.Length
Dim myStream As Stream = myWebReq.GetRequestStream()
myStream.Write(data, 0, data.Length)
myStream.Close()
myWebResp = myWebReq.GetResponse
sr = New StreamReader(myWebResp.GetResponseStream)
Dim strHTML As String = sr.ReadToEnd
sw = File.CreateText("d:\Downloads\1.htm")
sw.WriteLine(strHTML)
sw.Close()
Response.WriteFile("d:\Downloads\1.htm")

推荐答案

" buran" < BU *** @ buran.com>在消息中写道

新闻:OJ ************** @ TK2MSFTNGP09.phx.gbl ...
"buran" <bu***@buran.com> wrote in message
news:OJ**************@TK2MSFTNGP09.phx.gbl...
亲爱的ASP.NET程序员,

如何将数据发布到ASP.NET登录页面并通过身份验证?
登录页面使用表格
身份验证,用户必须提供用户名和密码,并且必须在提交按钮上单击
。我希望通过使用HttpWebRequest提供值来自动执行此过程,然后在网站上下载文件。我认为我无法调用提交按钮。 Pleeeasee帮忙,
提前谢谢
Dear ASP.NET Programmers,

How can I post data to an ASP.NET login page and pass authentication? The
login page uses forms
authentication, users must supply usernames and password and have to click
on a submit button. I
want to automate this process by supplying values with HttpWebRequest and
then download a file on
the site. I think that I cannot invoke the submit button. Pleeeasee help,
thanks in advance




我发誓我之前回答了这个问题...


你正试图用代码代替浏览器。你的代码将不得不执行浏览器所做的事情:


1)浏览器尝试访问受保护的页面,例如protected.aspx

2)表单身份验证看到请求在适当的cookie中不包含Forms

身份验证票证,因此服务器回复

" ; 302 Page Moved status和一个指向登录页面的Location标题。

3)浏览器请求指定的登录页面。

4)服务器响应登录页面

5)用户填写登录页面并提交表格,因此浏览器

将表格发回登录页面

6)如果凭证有效,服务器以另一个302 Page

Moved响应。 status,一个指向protected.aspx的Location头,以及一个提供加密的Forms身份验证票据的
Set-Cookie头。

7)然后浏览器请求protected.aspx,但是这次在Cookie标题中提供了

表单身份验证票证

8)表单身份验证看到请求包含有效的表单

身份验证票证,所以它允许访问。服务器以受保护的页面回复




这是您的代码需要复制的内容。

-

John Saunders

互联网工程师
jo ** *********@surfcontrol.com


嗨Buran。


我不喜欢不知道这是否真的与VB.net有关,但它可以。


如果我理解你,你想通过用户认证

谁已经登录过一次。


这是用cookie完成的,所以你为什么不使用它。


我知道如何用vb.script和JScript做到这一点,但我从来没有用

vb.net做到这一点。


当这是你想要的,你想要的使用VB.net aspx.vb和

来做你无法弄清楚如何。

消息比明天再消息。

也许我可以帮忙然后你在寻找溶剂。


我做了不要尝试,因为我的电脑最后几天出了问题。

Cor


PS。如果它是主题为VB.net

语言webform身份验证问题的VB.net语言问题,则发布帖子并且只对这个新闻组。
Hi Buran.

I don''t know if this is really VB.net related, but it can be.

If I understand you right you want to pass the authentication from a user
who is already once before logged in.

That is normaly done with a cookie, so why are you not using that.

I know how to do it with vb.script and JScript but I never did it with
vb.net.

When this is what you want and you want to do it using VB.net aspx.vb and
you cannot figured out how.
Message than tomorrow again.
Maybe I can help you then searching for the sollution.

I did not try it because I had the last days problems with my computer.
Cor

PS. post if it is a VB.net language problem with the subject "VB.net
language webform authentication problem" and only to this newsgroup.


是的,这就是我想要做的。如果你帮助过我,我会很感激。

谢谢...


Buran

" Cor" < no*@non.com>在消息中写道

news:3f *********************** @ reader21.wxs.nl ...
Yes, that''s what I want to do. I would be thankful if you helped me.
Thanks...

Buran
"Cor" <no*@non.com> wrote in message
news:3f***********************@reader21.wxs.nl...
嗨Buran。

我不知道这是否真的与VB.net有关,但它可以。

如果我理解你就是你想要的从登录之前已经登录过的用户那里传递身份验证。

这是用cookie完成的,所以你为什么不使用它。
我知道如何用vb.script和JScript做到这一点,但我从来没有用
vb.net做过。

当这是你想要的,你想用VB做。 net aspx.vb和
你无法弄清楚如何。
消息比明天还要多。
也许我可以帮助你然后寻找解决方案。

我没有试试看,因为我的电脑最后几天出了问题。

Cor

PS。如果它是主题为VB.net
语言webform身份验证问题的VB.net语言问题,则发布帖子并且只对这个新闻组。
Hi Buran.

I don''t know if this is really VB.net related, but it can be.

If I understand you right you want to pass the authentication from a user
who is already once before logged in.

That is normaly done with a cookie, so why are you not using that.

I know how to do it with vb.script and JScript but I never did it with
vb.net.

When this is what you want and you want to do it using VB.net aspx.vb and
you cannot figured out how.
Message than tomorrow again.
Maybe I can help you then searching for the sollution.

I did not try it because I had the last days problems with my computer.
Cor

PS. post if it is a VB.net language problem with the subject "VB.net
language webform authentication problem" and only to this newsgroup.



这篇关于HttpWebRequest并发布登录数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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