0

I've got some Python code I'm trying to convert to vb.net but I'm getting hung up. The Python code is just one line where I am talking to a controller via a web connection:

connection.request(method, str(proxy + uri), data, authHeader)

The method is "GET", the str(proxy + uri) is "/file/cfg/equipment.role?Id=20", and the data and authHeader are empty. The connection.host property is 198.80.18.1. The line of code in Python works just fine.

In Vb.net my code is:

Dim request As WebRequest = WebRequest.Create(sHRef)
request.Credentials = CredentialCache.DefaultCredentials
Dim response As WebResponse = request.GetResponse()

sHref is a string equal to "http://198.80.18.1/file/cfg/equipment.role?Id=20"

But I keep getting an error on the last line of code to get a response: "The remote server returned an error: (404) Not Found."

I feel like I'm missing something easy, but haven't figured it out yet. Anyone got a better set of eyes than me?

Jason Shoulders
  • 659
  • 1
  • 12
  • 24
  • Could be that the python version sets some headers that teh C# version does not. To debug this you really need to get a view on the difference between the outgoing requests. Use Wireshark to capture the conversation and examine the differences. Side note, did you catch the remark in MSDN for WebRequest saying *We don't recommend that you use WebRequest or its derived classes for new development. Instead, use the System.Net.Http.HttpClient class.* – Caius Jard Mar 18 '20 at 20:39
  • When I use Wire Shark, it looks darn near the same: Good https://i.imgur.com/y2oHxzk.png Bad https://i.imgur.com/FU94usA.png – Jason Shoulders Mar 18 '20 at 21:21
  • That's not the entire request. Eg look at the screenshot here: https://subscription.packtpub.com/book/networking_and_servers/9781785887819/5/ch05lvl1sec35/http - see the "Host/Connection/Accept/UserAgent" headers etc... You want to be diffing those – Caius Jard Mar 18 '20 at 21:45
  • There's *something* different about the requests cos they're different lengths (147 vs 144) – Caius Jard Mar 18 '20 at 21:49
  • I didn't even notice those lengths being different. I'm at home now but I will see if I can drill down to that level tomorrow morning. I appreciate you taking the time to help me out with this. – Jason Shoulders Mar 19 '20 at 01:36
  • When I look at the Hypertext Transfer Protocol for the one that's working, the GET command matches, Host matches, URI matches. There is a header difference. The one that is working has Accept-Encoding: identity while the one that's not working had Connection: Keep-alive. I can ADD a header to my vb.net code to have Accept-Encoding but unfortunately I can not yet figure out how to take out the keep-alive. I tried the header remove function but when I do that, it adds a LOT of new variables to the header I don't want. So, I've been messing with it for a while trying to make them match. – Jason Shoulders Mar 19 '20 at 18:05
  • Rather than getting them to be identical, try just adding the Accept-Encoding header and see – Caius Jard Mar 19 '20 at 19:48
  • PS, use HttpClient. PPS: https://stackoverflow.com/questions/47411524/how-to-prevent-httpclient-from-sending-the-connection-header – Caius Jard Mar 19 '20 at 19:52
  • I did add the Accept-Encoder and it didn't work so I assumed the reason it's still not working is because of the Keep-alive. I'm talking to a controller via the web service so it makes sense to me that it might not know how to handle that keep-alive header. Although I obviously can't say for sure until I actually get it working. My main project is in Studio 2008 so I can't easily use the HttpClient. I went ahead and created a new project in 2019 and am trying to use the HttpClient. My first attempt it didn't work and based on your link, it looks like there may not be an easy way to do it. – Jason Shoulders Mar 19 '20 at 19:58
  • Make it connection close rather than trying to hide the header (not easy) – Caius Jard Mar 19 '20 at 21:50
  • Ps one url was requesting is 48 and the other was 49. Is it significant? Some web services return 404 when they mean "I couldn't find a user with that id" - it doesn't necessarily mean "there isn't a document/path/whatever on the web server" – Caius Jard Mar 19 '20 at 21:50
  • I thought I had tried the connection close before and it didn't work but I've tried so many things I should try again to be sure. The URL POST request does have a slightly different look but the controller responds basically the same for both so I didn't try to clean up that difference. It responds with a path to try to get the file from. The only thing that's different is the id. The id value will increment with each request. I need to take a bit of a break because this problem has worn me down. I will hopefully dive back into on Monday or so. Thanks again! :) – Jason Shoulders Mar 20 '20 at 19:13
  • Might be time to have a go at using the service from external webservice test app like Postman and see if you can get it working there.. just watch it turn out to be something prove – Caius Jard Mar 20 '20 at 19:19

0 Answers0