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

VS 2012 [RESOLVED] WebClient PostData Error

$
0
0
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

Viewing all articles
Browse latest Browse all 27348

Trending Articles



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