HttpWebRequest only works on .NET 4.0

I am having a strange problem or even behavior using WebRequest. First of all, here is what I am 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 Framework 3.5 installed in my compilation settings will result in a timeout in:

str2 = req.GetRequestStream()

when setting up Framework version 4.0 it works, and everything passes without any timeout problem. Does anyone know why this is happening? I tried 3.0 too, and it didn't work either.

(In this example, I am using VB.NET, but C # solutions are also welcome.)

+5
source share
2 answers

, , . , using, ( , IDisposable), .

using (var stream = req.GetRequestStream())
{
    ...
}

, , .

, , .NET Framework, (, URL ):

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)

. .NET 4.0 3.5. , Fiddler:

POST someurl HTTP/1.1
: application/xml; q = 0.9,/; q = 0.8
Content-Type: application/x-www-form-urlencoded
: someurl
Content-Length: 12
: 100-continue
: Keep-Alive

= DoStuff

+2

IL- exe , ILSpy.
, , .

0

All Articles