1
votes

I have seen a few other threads on this but haven't found a solution. When running my C# Windows From from debug mode, I am able to use the WebClient to read data from a web page. However, as soon as I create the executable for the application, I am unable to read the data from the web page. Here the exception that I am getting:

System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a send. ---> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags) at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)

---End of inner exception stack trace ---

Here is a snippet of the code I am using:

WebClient webClient = new WebClient();
webClient.UseDefaultCredentials = true;
webClient.Proxy.Credentials = CredentialCache.DefaultCredentials;
webClient.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36");
string page = webClient.DownloadString(webPath + manPartNum);

Like I said previously, I am able to get this to work correctly in debug mode of visual studio but as soon as I create the executable, I am no longer able to read from the web page. Any help would be appreciated. What could be causing this issue?

2
There are few possibilities. 1) All you changes did not get into the executable due t a dependency issue with the compiler. So you can delete the bin folder which will completely rebuild project so the release mode will get rebuilt 2) The code works first time.But 2nd time a cookie was established and there is something wrong with running code with cookie.So you can open an IE and delete history including cookies and try again.Or go back to the debug version and see if it still works.3) Your old connection is not closed and server isn't allowing more than one connection from same IP address.jdweng
Wouldn't UseDefaultCredentials and your supplying current user credentials be in conflict? I'd look in the output window when you're "succeeding" in debug to be sure it's not throwing there as well, but letting you get away with it anyhow.DonBoitnott
Thanks for the quick response. I tried your suggestions. 1) I deleted the bin folder and completely rebuilt the project. No success. 2) I re-ran the program in debug mode and it still works correctly in debug mode. 3) Is there a way for me to close the webclient? weblient.Close()?Tyler Wilson
@DonBoitnott - You are the man! Thanks for your help. That was my issue. I removed the two lines of default credentials and it is working now.Tyler Wilson
I got excited too soon. It appears that this problem is intermittent. It worked on my machine but when I used the executable on a different machine, it had the same error that I was seeing previously.Tyler Wilson

2 Answers

0
votes

An alternative would be to use an HttpWebRequest instead:

String responseFromServer = null;
var url = webPath + manPartNum;
var request = (HttpWebRequest)HttpWebRequest.Create(urlPing);
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36";
var response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
    using (var dataStream = response.GetResponseStream())
    {
        using (var reader = new StreamReader(dataStream))
            responseFromServer = reader.ReadToEnd();
    }
}

if (!String.IsNullOrEmpty(responseFromServer))
{
}

Unless you are truly "loggin in" to the page, your use of WebClient might be unnecessary. If all you need is to request the page response, then this should avoid all of those authentication headaches.

0
votes

After doing more research, I was able to determine that the website that I was attempting to access has OAuth 2.0 Authentication and Authorization. It looks like I must register with the company and use their API to access their website with my application. Hopefully this helps someone else.