HTTPWebRequest无法获取数据请帮忙 [英] HTTPWebRequest cant get data please help

查看:124
本文介绍了HTTPWebRequest无法获取数据请帮忙的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

点击后,

https://www.noridianmedicare.com/eula.php?t=/p-medb/enroll/optout/alaska_optout.html%3f


我同意锚点,你将被带到包含数据的页面......我想要来自C#代码的数据。


为此我编写了以下代码:


 Private Sub Functio()
Dim txtContent As New StringBuilder("")
txtHeader。 Text =""

'通过指定URL
wReq = DirectCast(HttpWebRequest.Create(" https://www.noridianmedicare.com/eula.php"))将Web请求发送到服务器, HttpWebRequest)

wReq.Method =" POST"
Dim str As String =" t =%2Fp-medb%2Fenroll%2Foptout%2Falaska_optout.html%253f& agree = 1"
wReq.ContentType =" application / x-www-form-urlencoded"
Dim bytedata As Byte()= Encoding.UTF8.GetBytes(str)
wReq.ContentLength = bytedata.Length

'不需要持续连接
wReq .KeepAlive = True

sStream = wReq.GetRequestStream()
sStream.Write(bytedata,0,bytedata.Length)
sStream.Close()

''将我们引荐到URL的链接
'wReq.Referer = txtReferrer.Text
''浏览器的用户代理
'wReq.UserAgent = txtUserAgent.Text
'从服务器获取响应
wResp = DirectCast(wReq.GetResponse(),HttpWebResponse)
'显示响应的标题
txtHeader.Text = wResp.Headers.ToString( )

'获取一个流来读取响应的主体
rStream = wResp.GetResponseStream()
'需要循环缓冲区
Dim bufCount As Integer = 0
'缓冲区,我们将在其中存储来自se的数据rver
Dim byteBuf As Byte()= New Byte(1023){}
'循环只要缓冲区中有数据
Do
'用数据$ b填充缓冲区$ b bufCount = rStream.Read(byteBuf,0,byteBuf.Length)

如果bufCount<> 0然后
'将字节转换为ASCII文本并附加到文本框
txtContent.Append(Encoding.ASCII.GetString(byteBuf,0,bufCount))
End if
Loop虽然bufCount> 0

txtbContent.Text = txtContent.ToString()
End Sub


我缺少什么?

解决方案

从简单开始并使用此处的技术来解决工作(IE)和非工作(您的代码)案例:


http://blogs.msdn.com/b/jpsanders/archive/2008/06/25/troubleshooting-code-that-uses-the-http-protocol.aspx


https://www.noridianmedicare.com/eula.php?t=/p-medb/enroll/optout/alaska_optout.html%3f

after you click the I Agree anchor, you will be taken to the page containing the data... i want that data from C# code.

for that I wrote the following code:

  Private Sub Functio()
    Dim txtContent As New StringBuilder("")
    txtHeader.Text = ""

    ' Place the web request to the server by specifying the URL
    wReq = DirectCast(HttpWebRequest.Create("https://www.noridianmedicare.com/eula.php"), HttpWebRequest)

    wReq.Method = "POST"
    Dim str As String = "t=%2Fp-medb%2Fenroll%2Foptout%2Falaska_optout.html%253f&agree=1"
    wReq.ContentType = "application/x-www-form-urlencoded"
    Dim bytedata As Byte() = Encoding.UTF8.GetBytes(str)
    wReq.ContentLength = bytedata.Length

    ' No need for a persistant connection
    wReq.KeepAlive = True

    sStream = wReq.GetRequestStream()
    sStream.Write(bytedata, 0, bytedata.Length)
    sStream.Close()

    '' The link that referred us to the URL
    'wReq.Referer = txtReferrer.Text
    '' The user agent of the browser
    'wReq.UserAgent = txtUserAgent.Text
    ' Get the response from the server
    wResp = DirectCast(wReq.GetResponse(), HttpWebResponse)
    ' Display the header of the response
    txtHeader.Text = wResp.Headers.ToString()

    ' Get a stream to read the body of the response
    rStream = wResp.GetResponseStream()
    ' Needed for looping through the buffer
    Dim bufCount As Integer = 0
    ' Buffer in which we're going to store data coming from the server
    Dim byteBuf As Byte() = New Byte(1023) {}
    ' Loop as long as there's data in the buffer
    Do
      ' Fill the buffer with data
      bufCount = rStream.Read(byteBuf, 0, byteBuf.Length)

      If bufCount <> 0 Then
        ' Transform the bytes into ASCII text and append to the textbox
        txtContent.Append(Encoding.ASCII.GetString(byteBuf, 0, bufCount))
      End If
    Loop While bufCount > 0

    txtbContent.Text = txtContent.ToString()
  End Sub

what am I missing?

解决方案

Start simple and use the techniques here to troubleshoot the working (IE) and non-working (your code) cases:

http://blogs.msdn.com/b/jpsanders/archive/2008/06/25/troubleshooting-code-that-uses-the-http-protocol.aspx


这篇关于HTTPWebRequest无法获取数据请帮忙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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