Joeri Akkerman
Joeri Akkerman

Reputation: 34

SocketException when trying to do a HTTPRequest in C#

I've made a Windows Forms Application for the company I work for. On my own laptop and PC, the program works fine, but when I tried to install the program on my company's computer things went very wrong.

I created the program with .NET Framework 4.6.1 and the current Framework on the PC at the company I work for is version 4.0. Because of this difference in version I got some errors. I've managed to fix all the errors except for one...

I want to make a backup from the local stored data on a webserver (000webhost server). Again, all of this works fine on my own laptop, even when I connect to the WIFI at my company, on my own machine it works fine. But on the company's PC things went south.. I keep getting an exception when trying to make a back-up.

I've searched on google for any solutions, but unfortunately without any luck. There was only one result that told me anything about this exception, the guy wrote that the HttpClient object should be static and I shouldn't be using a using() clause.

My current code:

private static HttpClient client;
public Parser(Form1 form)
{
      client = new HttpClient();
}




private async Task<int> backup()
    {
        try
        {
                var data = new FormUrlEncodedContent(new[]
                {
                    new KeyValuePair<string, string>("license", license),
                    new KeyValuePair<string, string>("json", text),
                    new KeyValuePair<string, string>("date", DateTime.Today.ToString("dd/MM/yyyy"))
                });
                var response = await client.PostAsync(URL + "makebackup.php", data);
                string content = await response.Content.ReadAsStringAsync();
                Response result = JsonConvert.DeserializeObject<Response>(content);
                return result.result;
        }
        catch (HttpRequestException)
        {
            form.showMessageBox("Kon geen backup maken door het ontbreken van een internet verbinding");
        }
        return -1;
    }

public async Task<CheckVersionResult> checkVersion()
        {
            try
            {
                var data = new FormUrlEncodedContent(new[]
                {
                    new KeyValuePair<string, string>("version", form.mrtVersion)
                });
                var response = await client.PostAsync(URL + "checkversion.php", data);
                string content = await response.Content.ReadAsStringAsync();
                CheckVersionResult result = JsonConvert.DeserializeObject<CheckVersionResult>(content);
                return result;
            }
            catch (HttpRequestException)
            {
                form.showMessageBox("Je hebt een internetverbinding nodig om dit programma te kunnen gebruiken");
                return null;
            }
        }

Unfortunately the error message was written in dutch, so most of you are not able to read the full error message.. The most important line is probably the first of the stacktrace which is this translated in English: "SocketException (0x80004005): An invalid argument was supplied".

Error message

Zie het einde van dit bericht voor meer informatie over het aanroepen 
van JIT-foutopsporing (Just In Time) in plaats van dit dialoogvenster.
************** Tekst van uitzondering **************
System.Net.Sockets.SocketException (0x80004005): Er is een ongeldig argument opgegeven
   bij System.Net.SafeCloseSocketAndEvent.CreateWSASocketWithEvent(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType, Boolean autoReset, Boolean signaled)
   bij System.Net.NetworkAddressChangePolled..ctor()
   bij System.Net.AutoWebProxyScriptEngine.AutoDetector.Initialize()
   bij System.Net.AutoWebProxyScriptEngine.AutoDetector.get_CurrentAutoDetector()
   bij System.Net.AutoWebProxyScriptEngine..ctor(WebProxy proxy, Boolean useRegistry)
   bij System.Net.WebProxy.UnsafeUpdateFromRegistry()
   bij System.Net.Configuration.DefaultProxySectionInternal..ctor(DefaultProxySection section)
   bij System.Net.Configuration.DefaultProxySectionInternal.GetSection()

************** Geladen assembly's **************
mscorlib
    Assembly-versie: 4.0.0.0
    Win32-versie: 4.7.2117.0 built by: NET47REL1LAST
    CodeBase: file:///C:/Windows/Microsoft.NET/Framework64/v4.0.30319/mscorlib.dll
----------------------------------------
MRT
    Assembly-versie: 1.0.0.0
    Win32-versie: 1.0.0.0
    CodeBase: file:///O:/DKW/MRT%20(Medewerkers%20Resultaten%20Tool)/release%202/MRT.exe
----------------------------------------
System.Windows.Forms
    Assembly-versie: 4.0.0.0
    Win32-versie: 4.7.2114.0 built by: NET47REL1LAST
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System
    Assembly-versie: 4.0.0.0
    Win32-versie: 4.7.2114.0 built by: NET47REL1LAST
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Drawing
    Assembly-versie: 4.0.0.0
    Win32-versie: 4.7.2053.0 built by: NET47REL1
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System.Configuration
    Assembly-versie: 4.0.0.0
    Win32-versie: 4.7.2053.0 built by: NET47REL1
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Configuration/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
----------------------------------------
System.Core
    Assembly-versie: 4.0.0.0
    Win32-versie: 4.7.2117.0 built by: NET47REL1LAST
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Core/v4.0_4.0.0.0__b77a5c561934e089/System.Core.dll
----------------------------------------
System.Xml
    Assembly-versie: 4.0.0.0
    Win32-versie: 4.7.2102.0 built by: NET47REL1LAST
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Xml/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------
System.Windows.Forms.DataVisualization
    Assembly-versie: 4.0.0.0
    Win32-versie: 4.7.2053.0
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms.DataVisualization/v4.0_4.0.0.0__31bf3856ad364e35/System.Windows.Forms.DataVisualization.dll
----------------------------------------
CircularProgressBar
    Assembly-versie: 2.5.6403.13419
    Win32-versie: 2.5
    CodeBase: file:///O:/DKW/MRT%20(Medewerkers%20Resultaten%20Tool)/release%202/CircularProgressBar.DLL
----------------------------------------
WinFormAnimation
    Assembly-versie: 1.5.6298.3372
    Win32-versie: 1.5
    CodeBase: file:///O:/DKW/MRT%20(Medewerkers%20Resultaten%20Tool)/release%202/WinFormAnimation.DLL
----------------------------------------
System.Windows.Forms.DataVisualization.resources
    Assembly-versie: 4.0.0.0
    Win32-versie: 4.7.2053.0
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms.DataVisualization.resources/v4.0_4.0.0.0_nl_31bf3856ad364e35/System.Windows.Forms.DataVisualization.resources.dll
----------------------------------------
System.Net.Http
    Assembly-versie: 4.0.0.0
    Win32-versie: 4.7.2053.0 built by: NET47REL1
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Net.Http/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Net.Http.dll
----------------------------------------
Newtonsoft.Json
    Assembly-versie: 11.0.0.0
    Win32-versie: 11.0.2.21924
    CodeBase: file:///O:/DKW/MRT%20(Medewerkers%20Resultaten%20Tool)/release%202/Newtonsoft.Json.DLL
----------------------------------------
System.Numerics
    Assembly-versie: 4.0.0.0
    Win32-versie: 4.7.2053.0 built by: NET47REL1
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Numerics/v4.0_4.0.0.0__b77a5c561934e089/System.Numerics.dll
----------------------------------------
System.Runtime.Serialization
    Assembly-versie: 4.0.0.0
    Win32-versie: 4.7.2106.0 built by: NET47REL1LAST
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Runtime.Serialization/v4.0_4.0.0.0__b77a5c561934e089/System.Runtime.Serialization.dll
----------------------------------------
System.Data
    Assembly-versie: 4.0.0.0
    Win32-versie: 4.7.2102.0 built by: NET47REL1LAST
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_64/System.Data/v4.0_4.0.0.0__b77a5c561934e089/System.Data.dll
----------------------------------------
System.resources
    Assembly-versie: 4.0.0.0
    Win32-versie: 4.7.2053.0 built by: NET47REL1
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.resources/v4.0_4.0.0.0_nl_b77a5c561934e089/System.resources.dll
----------------------------------------
mscorlib.resources
    Assembly-versie: 4.0.0.0
    Win32-versie: 4.7.2053.0 built by: NET47REL1
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/mscorlib.resources/v4.0_4.0.0.0_nl_b77a5c561934e089/mscorlib.resources.dll
----------------------------------------
System.Windows.Forms.resources
    Assembly-versie: 4.0.0.0
    Win32-versie: 4.7.2053.0 built by: NET47REL1
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms.resources/v4.0_4.0.0.0_nl_b77a5c561934e089/System.Windows.Forms.resources.dll
----------------------------------------
************** JIT-foutopsporing **************
Als u JIT-foutopsporing wilt inschakelen, moet in het configuratiebestand voor deze
toepassing of computer (machine.config) de waarde
jitDebugging in het gedeelte system.windows.forms zijn ingesteld.
De toepassing moet ook zijn gecompileerd terwijl foutopsporing
was ingeschakeld.
Bijvoorbeeld:
<configuration>
    <system.windows.forms jitDebugging="true" />
</configuration>
Wanneer JIT-foutopsporing is ingeschakeld, worden onverwerkte uitzonderingen
naar het JIT-foutopsporingsprogramma gestuurd dat op de computer is geregistreerd
en worden niet door dit dialoogvenster verwerkt.

The company's PC is running windows 32 bit version 4.7.2117.0 built by: NET47REL1LAST. All of the functionalities are blocked by the administrator, so I'm not able to check anything about the system info and I'm not able to install anything on this machine unfortunately... My guess is that the issue has something to do with the .NET Framework version, but I may be wrong...

Any help would be appreciated! Thanks in advance! Joeri.

Upvotes: 0

Views: 658

Answers (1)

Oscar Itaba
Oscar Itaba

Reputation: 24

.Net 4.0 doesn't support async await upgrade to 4.5 or above and it will work fine. I believe your local pc is running on a higher version of .net that is why the application run without any issues on your personal pc

Upvotes: 1

Related Questions