0

I am going to integrate an existing c# project into a legacy VB.net system. the below code works in C# perfectly

private string GETShopify(string CallLoc, string APIKey, string Password, string StoreName)
    {
        string Str = APIKey + ":" + Password;
        byte[] byt = System.Text.Encoding.UTF8.GetBytes(Str);
        Str = System.Convert.ToBase64String(byt);

        string Url = "https://" + StoreName + ".myshopify.com/admin/" + CallLoc;
        WebRequest request = WebRequest.Create(Url);

        NetworkCredential SimpleCredential = new NetworkCredential(APIKey, Password);
        request.Credentials = SimpleCredential;

        request.Headers.Add("Authorization", "Basic " + Str);
        request.ContentType = "application/json";
        request.Method = "GET";

        WebResponse response = request.GetResponse();

    }

However VB is failing on GetResponce

 Private Function GETShopify(ByVal CallLoc As String, ByVal APIKey As String, ByVal Password As String, ByVal StoreName As String) As String
    Dim Str As String = APIKey & ":" & Password
    Dim byt As Byte() = System.Text.Encoding.UTF8.GetBytes(Str)
    Str = System.Convert.ToBase64String(byt)

    Dim Url As String = "https://" & StoreName & ".myshopify.com/admin/" & CallLoc
    Dim request As WebRequest = WebRequest.Create(Url)
    Dim SimpleCredential As NetworkCredential = New NetworkCredential(APIKey, Password)
    request.Credentials = SimpleCredential
    request.Headers.Add("Authorization", "Basic " & Str)
    request.ContentType = "application/json"
    request.Method = "GET"
    Dim response As WebResponse = request.GetResponse() <<FAIL

End Function
 System.Net.WebException   HResult=0x80131509   Message=The underlying connection was closed: An unexpected error occurred on a send.

Inner Exception 1: IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.

Inner Exception 2: SocketException: An existing connection was forcibly closed by the remote host


I cannot find a cause for this. the code is extremely simple so wouldnt imagine this much of an issue.

If anyone has ever hit this issue before or has any suggestions that would be fantastic.

Yogesh Patel
  • 818
  • 2
  • 12
  • 26
  • What error are you getting ? – auburg Nov 27 '18 at 11:42
  • System.Net.WebException Message=The underlying connection was closed: An unexpected error occurred on a send. Inner Exception 1: IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. Inner Exception 2: SocketException: An existing connection was forcibly closed by the remote host – Aaron Nesbitt Nov 27 '18 at 11:44
  • Please edit your question with this error rather than as a comment – auburg Nov 27 '18 at 11:46
  • See if this post helps you https://stackoverflow.com/questions/22627977/the-underlying-connection-was-closed-an-unexpected-error-occurred-on-a-send – auburg Nov 27 '18 at 11:49
  • I'd bet you are running on an old .NET runtime that doesn't support TLS1.2. TLS1.2 is used by default after 4.6. We are currently at 4.7.2. The real solution is to target a recent runtime. The hack would be to specify TLS1.2 explicitly but that will cause the code to break when servers start moving/requiring TLS1.3 – Panagiotis Kanavos Nov 27 '18 at 11:52
  • Another culprit may be an outdated/unsupported OS like Windows 2008 R2. Older OSs require specific updates and registry keys to enable TLS1.2 – Panagiotis Kanavos Nov 27 '18 at 11:54
  • Changing the target framework (currently 4.0) to 4.6 fixes the problem. However as this is a legacy system this may cause a hornets nest. is it possible to run this action in 4.0? – Aaron Nesbitt Nov 27 '18 at 11:57
  • This is, more or less, the whole story about SSL protocols and registry settings in Windows (MSDN Source - For *older* systems): [TLS/SSL Settings](https://learn.microsoft.com/en-us/previous-versions/windows/it-pro/windows-server-2012-R2-and-2012/dn786418(v=ws.11)#BKMK_SchannelTR_TLS12). – Jimi Nov 27 '18 at 16:49
  • The machine where the application is running needs to have, at least, the .net 4.5 `System.dll` installed. Otherwise, .net 4.0 doesn't know how to "talk" TLS 1.2. If you can't re-target the Framework, force the `ServicePointManager` to include TLS 1.2 among the protocols used in the SSL handshake: `ServicePointManager.SecurityProtocol = CType(3072, SecurityProtocolType)`, where the "magic" 3072 is simply the numeric value of the enum corresponding to `SecurityProtocolType.Tls12`, since .NET 4.0 does not offer that choice. – Jimi Nov 27 '18 at 16:49
  • .Net 4.6.1 would be better if the platform supports it. – Jimi Nov 27 '18 at 16:55

0 Answers0