Solved

How to send HTTPWebRequest in asynchronous mode

Posted on 2014-03-12
2
353 Views
Last Modified: 2014-05-21
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
Comment
Question by:klheitz
2 Comments
 
LVL 21

Accepted Solution

by:
Craig Wagner earned 250 total points
Comment Utility
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
 
LVL 10

Assisted Solution

by:Monica P
Monica P earned 250 total points
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now