Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with HttpWebRequest

Status
Not open for further replies.

mcadphd

Programmer
Oct 20, 2011
4
US
I am sending USPS a zipcode value and pounds to get a return back for cost. If I just past the uri and values into browser i get back an xml with my RATE. In code when the request posts to the server I get an error back on my request.getresponsse and it tells me i received nothing back.

Code below ( this code works until the oResponse

Dim strAPIUserId As String = "?API=RateV4&XML=<RateV4Request USERID=""425HOBKN7132"">"
Dim strRevision As String = "<Revision>2</Revision>"
Dim strOpenPackage As String = "<Package ID=""0"">"
Dim strService As String = "<Service>PARCEL</Service>"
Dim strZipOrigination As String = "<ZipOrigination>47119</ZipOrigination>"

Dim strZipDestinationStart As String = "<ZipDestination>"
Dim strZipDestinationZip As String = 47172 'strShipToZipCode
Dim strZipDestinationEnd As String = "</ZipDestination>"

Dim strPoundsStart As String = "<Pounds>"
Dim strPounds As String = 2 'intQty
Dim strPoundsEnd As String = "</Pounds>"

Dim strOunces As String = "<Ounces>0</Ounces>"
Dim strContainer As String = "<Container></Container>"
Dim strSize As String = "<Size>REGULAR</Size>"
Dim strMachinable As String = "<Machinable>true</Machinable>"

Dim strClosePackage As String = "</Package>"
Dim strClosing As String = "</RateV4Request>"




Dim strFullString As String = strAPIUserId & strRevision & strOpenPackage & strService & strZipOrigination & _
strZipDestinationStart & strZipDestinationZip & strZipDestinationEnd & _
strPoundsStart & strPounds & strPoundsEnd & strOunces & strContainer & strSize & _
strMachinable & strClosePackage & strClosing



Try


Dim uri As New Uri(" & strFullString)


Dim data As String = "field-keywords=ASP.NET 2.0"
Dim request As HttpWebRequest = HttpWebRequest.Create(uri)

With request
.Method = WebRequestMethods.Http.Post
.ContentLength = data.Length
.ContentType = "application/x- End With

Dim writer As New StreamWriter(request.GetRequestStream)
With writer
.Write(data)
.Close()
End With

Dim oResponse As HttpWebResponse = request.GetResponse()
Dim reader As New StreamReader(oResponse.GetResponseStream())
Dim strReturnData As String = reader.ReadToEnd()
oResponse.Close()


Dim Document As New System.Xml.XmlDocument

Document.LoadXml(strReturnData)

ShippingCost = Document.SelectSingleNode("Rate").InnerText

End

Thanks
 
I set up the same thing a few years ago and if I recall it was a pain to get working correctly. I have posted the code I use, you can compare your code to it and you should be able to figure out the problem pretty easily. I put my result into a dataset and display it on a form so my users can chose the shipping method and rate they want.


Code:
 Public Function GenerateDomesticRateResult(ByVal xmlRequest As String) As DataSet
        Dim ServerName As String
        Dim DllName As String
        Dim request As HttpWebRequest
        Dim response As HttpWebResponse = Nothing
        Dim sr As StreamReader = Nothing
        Dim postdata As String

        Try
            ServerName = My.Settings.UspsServer
            DllName = "/ShippingAPI.dll?API=RateV3&XML="

            postdata = ServerName & DllName & xmlRequest
            request = CType(WebRequest.Create(postdata), HttpWebRequest)
            Try
                response = CType(request.GetResponse(), HttpWebResponse)
            Catch
                MsgBox("Failed to Connect to USPS Server Please Try again later, or contact Support", MsgBoxStyle.Critical, "Connection Failed")
            End Try

            sr = New StreamReader(response.GetResponseStream())

            Dim xmlRead As XmlTextReader
            xmlRead = New XmlTextReader(sr)

            Dim ds As DataSet = Nothing
            ds = New DataSet

            ds.ReadXml(xmlRead, XmlReadMode.Auto)
            Return ds
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error")
            Return Nothing
        Finally
            If sr IsNot Nothing Then
                sr.Close()
            End If
        End Try


    End Function

If you still have trouble let me know and I'll take some time to compare code.

Perrin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top