Solved

how to send webrequest in ASP.NET to USPS

Posted on 2008-06-25
3
998 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

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…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

752 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