I got a strange problem or even behaviour with a WebRequest. First and foremost, here's what I'm trying to do:
Dim req As HttpWebRequest = CType(Net.WebRequest.Create("https://cloud.myweb.de/myenginge/dostuff"), HttpWebRequest)
Dim inputString As String = "text=DoStuff"
Dim data As Byte() = System.Text.Encoding.ASCII.GetBytes(inputString)
req.Method = "POST"
req.Accept = "application/xml;q=0.9,*/*;q=0.8"
req.ContentType = "application/x-www-form-urlencoded"
req.ContentLength = data.Length
str2 = req.GetRequestStream()
str2.Write(data, 0, data.Length)
str2.Close()
Dim resp As HttpWebResponse = CType(req.GetResponse, HttpWebResponse)
str = resp.GetResponseStream()
buffer = New IO.StreamReader(str, System.Text.Encoding.ASCII).ReadToEnd
But having the .NET Frame 3.5 set in my compile settings will lead to a timeout in:
str2 = req.GetRequestStream()
while setting the Framework Version 4.0 works and everything passes without any timeout issue. Does anybody know why this happens? I tried 3.0 as well, and it didn't work either.
(I'm using VB.NET in this example, but C# solutions are also welcome.)
My guess is you have some other request sitting open which hasn't been disposed of yet. Update your code to use the using
statement where applicable (you should always use this when dealing with any objects which implement IDisposable
) e.g.
using (var stream = req.GetRequestStream())
{
...
}
This will make sure all streams are closed reliably before moving onto the next.
Update
This is definitely not an issue with switching the .NET Framework, I sandboxed your code into a small console app and re-wrote the code as follows (obviously switching out your URL for a different one):
Dim request = CType(WebRequest.Create("https://cloud.myweb.de/myenginge/dostuff"), HttpWebRequest)
Dim data As Byte() = System.Text.Encoding.ASCII.GetBytes("text=DoStuff")
request.Method = WebRequestMethods.Http.Post
request.Accept = "application/xml;q=0.9,*/*;q=0.8"
request.ContentType = "application/x-www-form-urlencoded"
request.ContentLength = data.Length
Using inputStream = request.GetRequestStream()
inputStream.Write(data, 0, data.Length)
End Using
Dim response = CType(request.GetResponse(), HttpWebResponse)
Dim buffer As String = ""
Using outputStream = response.GetResponseStream()
Using streamReader = New StreamReader(outputStream, System.Text.Encoding.ASCII)
buffer = streamReader.ReadToEnd()
End Using
End Using
Console.WriteLine(buffer)
And I got a successful response back everytime. I ran the same code under both .NET 4.0 & 3.5. Here's how each request looked courtesy of Fiddler:
POST someurl HTTP/1.1
Accept: application/xml;q=0.9,/;q=0.8
Content-Type: application/x-www-form-urlencoded
Host: someurl
Content-Length: 12
Expect: 100-continue
Connection: Keep-Alivetext=DoStuff
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With