Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 433
  • Last Modified:

How to send HTTPWebRequest in asynchronous mode

Hi Experts;
Below is my VB.net routine to send an HTTPWebRequest.  It is working perfectly as is, but I'd like the transmission to be asynchronous.
I'd like the simplest method to do this. The receiving url doesn't return any response.

Thanks

Public Sub SendMessage(strXMLIN As String, sDigSigIN As String, strURLIN As String)

        Dim req As HttpWebRequest = DirectCast(WebRequest.Create(strURLIN), HttpWebRequest)
        Dim encoding As New UTF8Encoding()
        Dim data As Byte() = encoding.GetBytes(strXMLIN)


        req = DirectCast(WebRequest.Create(strURLIN), HttpWebRequest)

        req.Method = "POST"
        req.ContentType = "application/x-www-form-urlencoded"
        req.ContentLength = data.Length
        req.ProtocolVersion = System.Net.HttpVersion.Version10
        req.Connection = False
        req.Headers.Set("hash", sDigSigIN)

        Dim strm As Stream = req.GetRequestStream()

        strm.Write(data, 0, data.Length)
        strm.Close()

        Dim response As HttpWebResponse = DirectCast(req.GetResponse(), HttpWebResponse)

        response.Close()

    End Sub
0
k heitz
Asked:
k heitz
2 Solutions
 
Craig WagnerSoftware ArchitectCommented:
This post should help you out. It uses the BeginGetResponse and EndGetResponse.

http://stackoverflow.com/a/202896/97382

If you're using .NET 4.5 you could use the HttpClient class which has a simpler async model built into it.
0
 
Monica PCommented:
check for more : http://www.matlus.com/httpwebrequest-asynchronous-programming/


Use HttpWebRequest.BeginGetResponse()

HttpWebRequest webRequest;

void StartWebRequest()
{
    webRequest.BeginGetResponse(new AsyncCallback(FinishWebRequest), null);
}

void FinishWebRequest(IAsyncResult result)
{
    webRequest.EndGetResponse(result);
}
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now