显示帖子数据!

发布于 2024-08-18 01:06:10 字数 1910 浏览 5 评论 0原文

我正在尝试将数据从 vb.net 应用程序发布到位于服务器上的 Web 服务 asmx!

为了从 vb.net 应用程序发布数据,我使用以下代码:

Public Function Post(ByVal url As String, ByVal data As String) As String
    Dim vystup As String = Nothing
    Try
        'Our postvars
        Dim buffer As Byte() = Encoding.ASCII.GetBytes(data)
        'Initialisation, we use localhost, change if appliable
        Dim WebReq As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest)
        'Our method is post, otherwise the buffer (postvars) would be useless
        WebReq.Method = "POST"
        'We use form contentType, for the postvars.
        WebReq.ContentType = "application/x-www-form-urlencoded"
        'The length of the buffer (postvars) is used as contentlength.
        WebReq.ContentLength = buffer.Length
        'We open a stream for writing the postvars
        Dim PostData As Stream = WebReq.GetRequestStream()
        'Now we write, and afterwards, we close. Closing is always important!
        PostData.Write(buffer, 0, buffer.Length)
        PostData.Close()
        'Get the response handle, we have no true response yet!
        Dim WebResp As HttpWebResponse = DirectCast(WebReq.GetResponse(), HttpWebResponse)
        'Let's show some information about the response
        Console.WriteLine(WebResp.StatusCode)
        Console.WriteLine(WebResp.Server)

        'Now, we read the response (the string), and output it.
        Dim Answer As Stream = WebResp.GetResponseStream()
        Dim _Answer As New StreamReader(Answer)

        'Congratulations, you just requested your first POST page, you
        'can now start logging into most login forms, with your application
        'Or other examples.
        vystup = _Answer.ReadToEnd()
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try

    Return vystup.Trim() & vbLf
End Function 

现在我如何在 asmx 服务中检索此数据?

I am trying to post data from vb.net application to web service asmx that is located on server!

For posting data from vb.net application I am using this code:

Public Function Post(ByVal url As String, ByVal data As String) As String
    Dim vystup As String = Nothing
    Try
        'Our postvars
        Dim buffer As Byte() = Encoding.ASCII.GetBytes(data)
        'Initialisation, we use localhost, change if appliable
        Dim WebReq As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest)
        'Our method is post, otherwise the buffer (postvars) would be useless
        WebReq.Method = "POST"
        'We use form contentType, for the postvars.
        WebReq.ContentType = "application/x-www-form-urlencoded"
        'The length of the buffer (postvars) is used as contentlength.
        WebReq.ContentLength = buffer.Length
        'We open a stream for writing the postvars
        Dim PostData As Stream = WebReq.GetRequestStream()
        'Now we write, and afterwards, we close. Closing is always important!
        PostData.Write(buffer, 0, buffer.Length)
        PostData.Close()
        'Get the response handle, we have no true response yet!
        Dim WebResp As HttpWebResponse = DirectCast(WebReq.GetResponse(), HttpWebResponse)
        'Let's show some information about the response
        Console.WriteLine(WebResp.StatusCode)
        Console.WriteLine(WebResp.Server)

        'Now, we read the response (the string), and output it.
        Dim Answer As Stream = WebResp.GetResponseStream()
        Dim _Answer As New StreamReader(Answer)

        'Congratulations, you just requested your first POST page, you
        'can now start logging into most login forms, with your application
        'Or other examples.
        vystup = _Answer.ReadToEnd()
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try

    Return vystup.Trim() & vbLf
End Function 

Now how i can retrieve this data in asmx service?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不打扰别人 2024-08-25 01:06:10

所有这些代码使得您看起来像是将数据发布到常规网页(可能是 .Net Web 表单),而不是 Web 服务。如果它是一个 Web 服务,您将改为传递 XML。因此,假设它是一个 .Net Web 表单,您可以使用 Request.Form("whatever-your-variable-is-known") 访问原始 POST 数据。但是,您似乎没有在 POST 数据中传递变量 = xyz,因此您需要在 Web 表单中访问原始 Request.InputStream

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim T As String
    Using SR As New System.IO.StreamReader(Request.InputStream)
        T = SR.ReadToEnd()
    End Using
End Sub

All of this code makes it look like you're posting data to a regular webpage, possibly a .Net Web Form, and not a web service. If it were a web service you'd be passing XML around instead. So assuming its a .Net Web Form you can access the raw POST data using Request.Form("whatever-your-variable-is-called"). However, it looks like you're not passing variable=xyz in the POST data so instead in your web form you're going to need to access the raw Request.InputStream

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim T As String
    Using SR As New System.IO.StreamReader(Request.InputStream)
        T = SR.ReadToEnd()
    End Using
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文