Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27346

HttpWebRequest only working with a Fiddler proxy?!

$
0
0
Hi VBForums, I will keep this short and too the point.

I am sending POST requests to a web service API using the HttpWebRequest/Response objects.

During testing, the POST requests we're working totally fine, and I was setting the WebRequest objects Proxy property to the Proxy I use to monitor HTTP traffic using fiddler.

However, this is the strange part. I removed the proxy because I was done testing/monitoring the webrequests. Now, when I send the POST request, the server returns an error saying "Length Required", indicated that the content length header is not set. (But I know that it is.)

When I set the Proxy back to my Fiddler proxy, it works just fine and the header is perfect.

This is so confusing because there should actually be no difference between sending the HttpWebRequest without setting the Proxy property and sending the HttpWebRequest using my local Fiddler proxy, except for I can monitor the requests with the latter.

If anything, I would expect the request to be successful WITHOUT setting the HttpWebRequests Proxy property, but it is only working if I do.

Here's some code with added comments to explain the problem with a bit of reference.

Code:

    Public Shared Username as String
    Public Shared Password as String
 
    Public Shared Function GetBalanceFromPostApi() As Double

        Dim responseString As String = ""

        Dim req As HttpWebRequest = HttpWebRequest.Create("http://postapi.example.com")
        req.Method = "POST"
        'The request is only successful if I set the requests Proxy property to my local Fiddler proxy.  If I comment out/remove the line below it will throw an exception when getting the requests response, saying that the content length of the header is required.  (Check below to verify that the requests content length header is indeed being set. )
        req.Proxy = New WebLib.Proxy("127.0.0.1:8888")
        req.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:22.0) Gecko/20100101 Firefox/22.0"
        req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
        req.ContentType = "multipart/form-data; boundary=---------------------------266271154912698"
        req.KeepAlive = True
        req.SendChunked = True
        req.Timeout = 30000

        Dim headerBody As New Text.StringBuilder

        headerBody.AppendLine("-----------------------------266271154912698")
        headerBody.AppendLine("Content-Disposition: form-data; name=""function""")
        headerBody.AppendLine("")
        headerBody.AppendLine("balance")
        headerBody.AppendLine("-----------------------------266271154912698")
        headerBody.AppendLine("Content-Disposition: form-data; name=""username""")
        headerBody.AppendLine("")
        headerBody.AppendLine(Username)
        headerBody.AppendLine("-----------------------------266271154912698")
        headerBody.AppendLine("Content-Disposition: form-data; name=""password""")
        headerBody.AppendLine("")
        headerBody.AppendLine(Password)
        headerBody.AppendLine("-----------------------------266271154912698--")

        Dim headerBodyBytes() As Byte = Text.Encoding.ASCII.GetBytes(headerBody.ToString)

        'See, the content length IS being set, and works only when sending the request through my local Fiddler proxy.
        req.ContentLength = headerBodyBytes.Length

        Using reqStream As IO.Stream = req.GetRequestStream
            reqStream.Write(headerBodyBytes, 0, headerBodyBytes.Length)
        End Using

        'Exception occurs below if the requests Proxy property is not set.
        Using resp As HttpWebResponse = req.GetResponse
            Using sr As New IO.StreamReader(resp.GetResponseStream)
                responseString = sr.ReadToEnd
            End Using
        End Using

        If responseString = "" Then
            Throw New ArgumentException("API returned no response.  Check username and password.")
        End If

        Dim balance As Double = responseString
        Return balance

    End Function


Viewing all articles
Browse latest Browse all 27346

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>