Вопрос

I have a WPF C# application that needs to connect to a remote SAP server. I'm running the application on two computers (same network), and I know that the server is reachable.

At first, computer A can connect fine using the web request (after making several tries, they all work fine). Randomly (it seems; I can't duplicate), all of the web requests will fail with the message "Unable to connect to the remote server" (type System.Net.WebException). Navigating to the URL in a browser loads fine; only the application cannot connect. The method that throws the exception is HttpWebRequest.GetRequestStream.

Even after restarting the application several times, all HttpWebRequests still fail to connect. We have multiple SAP servers, attempting to connect to any of them gives the same results. Meanwhile, the application on computer B (my development machine) can still connect to any of the servers with no problems. Only after restarting Computer A entirely, is it able to connect again.

This has happened on two different computers so far. Both times, the only way to resolve it was by restarting the computer.

Here's some code:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "text/xml; charset=utf-8";
request.ContentLength = contentLength;
request.Headers.Add("Authorization", GetAuthHeader());
request.Timeout = GetTimeout();

Stream post = request.GetRequestStream();//this line throws the exception
//...write to stream...
post.Close();

Edit: Overlooked the response part:

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream responsedata = response.GetResponseStream();
//...read all response data...
if (responsedata != null)
    responsedata.Close();

Edit2: So I commented out all of the calls to Close, and made a couple dozen requests. I also removed the code that gets the response and made several more requests. During these tests I received no exceptions and everything worked properly.

These results aside, I can understand something like this could break my program, but I don't understand why it would still be broken after restarting the program or trying to contact a different remote server. It seams like something is getting messed up in the .NET framework that doesn't resolve itself unless I restart the whole machine.

Это было полезно?

Решение

OP Here,

The Wireshark logs showed nothing when attempting to connect, so I assumed some other program was outright blocking my EXE from network access.

It turned out to be Bitdefender that was blocking it. I uninstalled that and everything started working. I don't know why it decided to start blocking it, but that's the answer.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top