1

I have search through the internet and Stack overflow for a solution to this issue and have tried various solutions but none of them has worked.

Basically, I have a Windows Application, written in VB.NET, when the button is clicked, it does the following:

 Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

    Dim data As Byte() = Encoding.UTF8.GetBytes("Some data message")

    Dim myHttpWebRequest As HttpWebRequest = HttpWebRequest.Create("https://SomeDomain.com/sandbox/update")

    'If required by the server, set the credentials.
    'myHttpWebRequest.Credentials = CredentialCache.DefaultCredentials

    Dim cert As X509Certificate = X509Certificate.CreateFromCertFile("C:\Program Files (x86)\Certificates\somecertificate.cer")
    System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
    myHttpWebRequest.ClientCertificates.Add(cert)

    myHttpWebRequest.ContentType = "application/json;profile=https://SomeDomain.com/update.json"

    myHttpWebRequest.Method = "POST"

    'WK 26/01/2017 - "2834"
    myHttpWebRequest.ContentLength = data.Length

    Dim oRequestStream = myHttpWebRequest.GetRequestStream()

    oRequestStream.Write(data, 0, data.Length)

    ' Get the response.
    Dim oWebResponse As WebResponse = myHttpWebRequest.GetResponse()

    ' Display the status.
    MsgBox(CType(oWebResponse, HttpWebResponse).StatusDescription)

    ' Get the stream containing content returned by the server.
    Dim oResponseStream As Stream = oWebResponse.GetResponseStream()

    ' Open the stream using a StreamReader for easy access.
    Dim oStreamReader As New StreamReader(oResponseStream)

    ' Read the content.
    Dim oResult As String = oStreamReader.ReadToEnd()

    ' Display the content.
    MsgBox(oResult)

    ' Clean up the streams and the response.
    oStreamReader.Close()
    oResponseStream.Close()

End Sub

However, it keeps throwing a "You must write ContentLength bytes to the request stream before calling [Begin]GetResponse" exception on this line:

 Dim oWebResponse As WebResponse = myHttpWebRequest.GetResponse()

The odd thing is, this code works on a different machine on the same network! The only difference is the machine is 32 bits and mine is 64 bits, not sure if that is related.

I'm not new to VB.NET but have not used HTTPWebRequest before, basically I'm trying to send some updates to a URL and trying to get the result back to see if it was successful or not. It requires a local certificate to be stored on my machine.

When I change the URL domain to something like "www.google.com" it works fine, but obviously that's not the URL I want to post to.

I just don't understand how it works on one machine but not another.

I have tried what other users suggested on similar post with using the "Using" keyword:

 Using oRequestStream = myHttpWebRequest.GetRequestStream()
            oRequestStream.Write(data, 0, data.Length)
        End Using

But then I get a "The remote server returned an error: (400) Bad Request." exception on the GetResponse line.

I've also tried adding the following to the config file:

<system.webServer>
    <security>
        <requestFiltering>
            <requestLimits maxAllowedContentLength="104857600" />
        </requestFiltering>
    </security>
</system.webServer>

but made no difference.

Please can someone kindly help me and give me some advice on this. Could it be to do with encoding difference on the machine?

Thank you.

will_k
  • 11
  • 2
  • Load up Fiddler and take a look at the request. See if it looks like what you expect. – Sam Axe Jan 30 '17 at 13:45
  • Hi, I can see in Fiddler: Tunnel to: http://somedomain.com:443 When I click on it and go to Raw: I see: HTTP/1.1 200 Connection Established FiddlerGateway: Direct StartTime: 16:19:10.861 Connection: close fiddler.network.https> HTTPS handshake to somedomain.com (for #39) failed. System.IO.IOException Authentication failed because the remote party has closed the transport stream. – will_k Jan 30 '17 at 16:20

0 Answers0