Solved

how to send webrequest in ASP.NET to USPS

Posted on 2008-06-25
3
994 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

820 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