1

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?

Tyler Wilson
  • 99
  • 3
  • 11
  • 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 Oct 11 '17 at 19:27
  • 1
    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 Oct 11 '17 at 19:59
  • 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 Oct 11 '17 at 20:20
  • @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 Oct 11 '17 at 20:37
  • 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 Oct 11 '17 at 21:05

2 Answers2

0

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.

DonBoitnott
  • 10,787
  • 6
  • 49
  • 68
  • Thanks again. However, I get the same error with HttpWebRequest as with WebClient. I think my problem has more to do with the website that I am trying to access. For example, I can access one distributor website but not another. The website seems to recognize that my application is a bot. – Tyler Wilson Oct 12 '17 at 14:31
  • @TylerWilson That and perhaps it limits similar connections by their frequency. The intermittent pattern you see might be because you're trying them too close together and it doesn't like that. DDoS prevention and the like. – DonBoitnott Oct 12 '17 at 15:09
  • Is there any way to get around this DDoS prevention? It is obviously there for a reason. Would adding delays in my code allow for this? – Tyler Wilson Oct 12 '17 at 16:01
  • 1
    Only someone who knows the site you're after could answer that. If there is, it probably requires logging in so the site knows who/what you are. Otherwise, you're just another potential attacker. – DonBoitnott Oct 12 '17 at 17:10
0

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.

Tyler Wilson
  • 99
  • 3
  • 11