Solved

how to send webrequest in ASP.NET to USPS

Posted on 2008-06-25
3
985 Views
Last Modified: 2013-11-26
I need to send request to receive response from USPS server.

I was doing in in ASP with the code given below but now i wants to send same USPS url using ASP.net (VB.net).Please provide me the sample code or reference .I am new in ASP.net and don't know how to send request to receive reply.


set xmlhttp = server.Createobject(MSXMLprogID)
      if err.number <> 0 then
            USPSrates = "USPS : " & err.Description
            exit function
      end if
      on error goto 0

      'Test server
      'xmlhttp.Open "POST","http://testing.shippingapis.com/ShippingAPITest.dll",false
      
      xmlhttp.Open "POST","http://production.shippingapis.com/ShippingAPI.dll",false
      xmlhttp.setRequestHeader "Content-Type","application/x-www-form-urlencoded"
      xmlhttp.send "API=" & API & "&XML=" & strXMLSend
      if xmlhttp.status <> 200 then
            USPSrates = "USPS : HTTP Error " & xmlhttp.status & " - " & xmlhttp.statusText & "."
            Set xmlhttp = nothing
            exit function
      end if
      strXMLRec = xmlhttp.responseText
      Set xmlhttp = nothing

      'Process return
      set xmlDom = Server.CreateObject(XMLDOMprogID)
      xmlDom.async = "false"
      xmlDom.LoadXML (strXMLRec)

Regards,
0
Comment
Question by:sdugar
  • 2
3 Comments
 
LVL 18

Accepted Solution

by:
jcoehoorn earned 500 total points
ID: 21865256
Don't use CreateObject in .Net.  Ever.
On Error style error handling isn't a good idea either.
The 'Set' keyword has a completely different meaning in VB.Net than it did in VB6/VBScript.

To send your web request, use System.Net.WebClient or System.Net.HttpWebRequest/System.Net.HttpWebRepsonse.

http://jcoehoorn.vox.com/library/post/dos-and-donts-for-vb6-programmers-using-net.html

0
 

Author Comment

by:sdugar
ID: 21865484
Dear jcoehoorn

Thanks for your reply.But i aman ASP programer and wants to know how to recreate the above ASP code in ASP.Net

i wants to know how to send request in ASP.net.Can you provide me sample code for this.

 'xmlhttp.Open "POST","http://testing.shippingapis.com/ShippingAPITest.dll",false
     
      xmlhttp.Open "POST","http://production.shippingapis.com/ShippingAPI.dll",false
      xmlhttp.setRequestHeader "Content-Type","application/x-www-form-urlencoded"
      xmlhttp.send "API=" & API & "&XML=" & strXMLSend


regards,
Sunny
0
 
LVL 18

Assisted Solution

by:jcoehoorn
jcoehoorn earned 500 total points
ID: 21865822
Remember, ASP.Net is not ASP.  What worked in ASP isn't necessarily a good idea in ASP.Net.  The pointers I gave you in the first post are all valid, and should be followed- don't ever use CreateObject for anything.  Don't use On Error.  Don't use the Set keyword.  If you want to write ASP.Net code, you need to understand how ASP.Net expects things to be done.
Dim wc As New System.Net.WebClient()

wc.Headers.Add(Net.HttpRequestHeader.ContentType, "application/x-www-form-urlencoded")

wc.QueryString.Add("API", API)

wc.QueryString.Add("XML", strXMLSend)
 

wc.DownloadString("http://production.shippingapis.com/ShippingAPI.dll")

Open in new window

0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

762 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

21 Experts available now in Live!

Get 1:1 Help Now