views:

93

answers:

1

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?

A: 

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
Chris Haas
You are right! This code for post data doesn't work propriety!Where i can find simple example that vb.net post data to asmx web service, and web service capture posting data?
Comii
Here's one from Microsoft:http://support.microsoft.com/kb/301273
Chris Haas