Results 1 to 3 of 3

Thread: [RESOLVED] WebClient PostData Error

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2011
    Location
    England
    Posts
    421

    Resolved [RESOLVED] WebClient PostData Error

    Hi I am trying to automate a few searches on http://www.192.com/businesses/advanced/ using the WebClient. I can get it to work using a WebBrowser which I would prefer to avoid for loading times. I got it to work using a WebRequest but would like to see if theres any reason why the WebClient code below keeps giving me the error: "The remote server returned an error: (500) Internal Server Error".

    This is the code using the WebClient:
    VB.NET Code:
    1. Dim WClient As New WebClient
    2.  
    3. ' // I got the headers from IE Developer Tools
    4. WClient.Headers.Add(HttpRequestHeader.UserAgent, "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)")
    5. WClient.Headers.Add(HttpRequestHeader.ContentType, "application/x-www-form-urlencoded")
    6. ' // This header is used when trying to compress the postdata using the commented code below
    7. ' WClient.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate")
    8.  
    9. ' // This is the postdata that is sent to the page as detected by IE Deleveloper Tools. I just add in the relevant search text i.e. company=company+name - with spaces replaced by +'s
    10. Dim Post As String = "company=&description=&subbuild=&buildno=&buildname=&street=&locality=&town=&county=&postcode=&btnSearch=Submit"
    11. Dim PostBytes As Byte() = Encoding.UTF8.GetBytes(Post)
    12.  
    13. ' // This part is commented out but just to show that I have tried using the ICSharpCode.SharpZipLib dll to compress the PostData before sending to the page
    14. ' Dim PostCompressedBytes As Byte() = Nothing
    15. ' Using PostDataStream As New MemoryStream
    16. '     Using PostCompress As New GZipOutputStream(PostDataStream)
    17. '         'PostCompress.IsStreamOwner = False
    18. '         PostCompress.Write(PostBytes, 0, PostBytes.Length)
    19. '     End Using
    20. '     PostCompressedBytes = PostDataStream.ToArray
    21. ' End Using
    22.  
    23. 'Now when I try to write the postdata to the page and read the response I get the error message
    24. Dim DocumentBytes As Byte() = WClient.UploadData("http://www.192.com/businesses/advanced/", PostBytes)

    I have also tried using a NameValueCollection to compile the postdata which also fails.

    I can get it to work using a WebRequest as below:
    VB.NET Code:
    1. Dim postData As String = "company=&description=&subbuild=&buildno=&buildname=&street=&locality=&town=&county=&postcode=&btnSearch=Submit"
    2. Dim postBytes As Byte() = Encoding.UTF8.GetBytes(postData)
    3. Dim request = CType(WebRequest.Create("http://www.192.com/businesses/search/advanced/"), HttpWebRequest)
    4. request.UserAgent = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)"
    5. request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate")
    6. request.Method = "POST"
    7. request.KeepAlive = True
    8. request.ContentType = "application/x-www-form-urlencoded"
    9. request.Referer = "http://www.192.com/businesses/search/advanced/"
    10. request.ContentLength = postBytes.Length
    11. request.AutomaticDecompression = DecompressionMethods.GZip Or DecompressionMethods.Deflate
    12.  
    13. Using postStream As Stream = request.GetRequestStream
    14.     postStream.Write(postBytes, 0, postBytes.Length)
    15. End Using
    16.  
    17. Using response = request.GetResponse()
    18.     Using responseStream = response.GetResponseStream()
    19.         Using sr = New StreamReader(responseStream)
    20.             MsgBox(sr.ReadToEnd())
    21.         End Using
    22.     End Using
    23. End Using

    Can anyone see if I have missed something in the WebClient approach or even let me know if it works for you so I know whether it is actually code related.

    Thanks
    Jay
    Last edited by JayJayson; May 10th, 2013 at 08:14 AM.

  2. #2
    Frenzied Member
    Join Date
    Nov 2005
    Posts
    1,834

    Re: WebClient PostData Error

    I think your problem is the url.

    In your WebClient code you navigate to :http://www.192.com/businesses/advanced/

    In your WebRequest code you navigate to: http://www.192.com/businesses/search/advanced/

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2011
    Location
    England
    Posts
    421

    Re: WebClient PostData Error

    Chris your absolutely right!

    It's a happy coincidence that I used that url at all in the WebRequest because that link does not contain all the input boxes to perform an advanced search. I guess it's a fluke that I did use it in the WebRequest and that the postdata remains the same.

    Thanks alot for your help. I wouldn't have tried that url without it being pointed out to me.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width