System.Net.WebException  
Author Message
ace333





PostPosted: .NET Framework Networking and Communication, System.Net.WebException Top

I'm using the following function to try to determine how long it takes for a file to download from a webserver.

private void btnDownload_Click(object sender, System.EventArgs e)

{

//****************************************************

Uri uri = new Uri(" http://www.hide-link.com/ #search=%22unix%20filetype%3Apdf%22");

System.Net.WebRequest wr = System.Net.HttpWebRequest.Create(uri);

System.IO.Stream stream = wr.GetResponse().GetResponseStream();

int b;

int counter=0;

string strResult = string.Empty;


DateTime BeforeDlTime = DateTime.Now;

while ((b=stream.ReadByte())!=-1)

{

strResult += System.Text.UTF7Encoding.UTF7.GetString(new byte[] {

(byte)b },0,1);

counter++;

Response.Write(counter);

}

DateTime AfterDlTime = DateTime.Now;

stream.Close();

//****************************************************

}

However I'm getting the following error: Googling at the moment but not sure how to fix it...

Error

The underlying connection was closed: The remote name could not be resolved.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Net.WebException: The underlying connection was closed: The remote name could not be resolved.

Source Error:

Line 55: Uri uri = new Uri(" http://www.hide-link.com/ #search=%22unix%20filetype%3Apdf%22");
Line 56: System.Net.WebRequest wr = System.Net.HttpWebRequest.Create(uri);
Line 57: System.IO.Stream stream = wr.GetResponse().GetResponseStream();
Line 58: int b;
Line 59: int counter=0;

Source File: c:\inetpub\wwwroot\downloadtime\webform1.aspx.cs Line: 57

Stack Trace:

[WebException: The underlying connection was closed: The remote name could not be resolved.]
System.Net.HttpWebRequest.CheckFinalStatus()
System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
System.Net.HttpWebRequest.GetResponse()
DownLoadTime.WebForm1.btnDownload_Click(Object sender, EventArgs e) in c:\inetpub\wwwroot\downloadtime\webform1.aspx.cs:57
System.Web.UI.WebControls.Button.OnClick(EventArgs e)
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
System.Web.UI.Page.ProcessRequestMain()


--------------------------------------------------------------------------------

Version Information: Microsoft .NET Framework Version:1.1.4322.2032; ASP.NET Version:1.1.4322.2032

Been googling for a while and can't find anything to help me fix this. This is an issue with my aspx webform and not a web service !





.NET Development27  
 
 
Ilya Tumanov





PostPosted: .NET Framework Networking and Communication, System.Net.WebException Top

It has exception message explainig why it happened. Install System.Sr.cab in your locale to see the actual message.

Probably would be something like “Can’t connect to the server” which means you’re having networking issues or you’re trying to use “localhost” as server name.



 
 
AndrewBadera





PostPosted: .NET Framework Networking and Communication, System.Net.WebException Top

I'd lop that %22 off the end of your URI -- that's a space, probably a leftover from a bad cut and paste

also, you should have some try/catch logic around the accessing of the response stream ...


 
 
TaylorMichaelL





PostPosted: .NET Framework Networking and Communication, System.Net.WebException Top

It is a network timeout issue communicating with the web service. I'd recommend running the report on the machine(s) having problems directly. In most cases you can do that by using IE to send the request directly to the web server from the client machine.

Michael Taylor - 2/1/07
http://p3net.mvps.org


 
 
Ameri





PostPosted: .NET Framework Networking and Communication, System.Net.WebException Top

Thanks Michael. I ran the query that was fetching the report in production and it took 230 seconds to complete and gave the report. Where can i find the timeout for this and increase it so that it does not timeout

-Ameri


 
 
ace333





PostPosted: .NET Framework Networking and Communication, System.Net.WebException Top

Wow, someone finally replied. Ya have taken your advice on both counts but still the same error :

System.Net.WebException: The underlying connection was closed: The remote name could not be resolved. at System.Net.HttpWebRequest.CheckFinalStatus() at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult) at System.Net.HttpWebRequest.GetResponse() at DownLoadTime.WebForm1.btnDownload_Click(Object sender, EventArgs e) in c:\inetpub\wwwroot\downloadtime\webform1.aspx.cs:line 62

Any other suggestions ... which is there such an issue with what seems is a simple task.



 
 
AndrewBadera





PostPosted: .NET Framework Networking and Communication, System.Net.WebException Top

are you able to connect to just http://cslibrary.stanford.edu via httpwebrequest, for starters

 

also, have you tried setting the Content-Type fo the request


 
 
Mike Flasko





PostPosted: .NET Framework Networking and Communication, System.Net.WebException Top

Is this running on .Net 2.0 If so, I suggest taking a system.net capture. This file should help you uncover what is causing the timeout (remote machine not responding, something on the local host, etc). Instructions are here: http://blogs.msdn.com/dgorti

 
 
TaylorMichaelL





PostPosted: .NET Framework Networking and Communication, System.Net.WebException Top

The timeout can be set through the TimeOut property on the web service proxy that you call. If you set it to -1 then it will never time out. You need to be careful with the timeout though as you don't want users waiting forever for a request that won't complete for some reason.

Michael Taylor - 2/15/07
http://p3net.mvps.org


 
 
ace333





PostPosted: .NET Framework Networking and Communication, System.Net.WebException Top

Thanks for the reply but no luck with the suggestion of using

("http://cslibrary.stanford.edu"); No matter what website I use it wont work. I think I need to find out some information about the proxy details of the company that I work for or else I have no chance of getting this little piece of code to run.



 
 
AndrewBadera





PostPosted: .NET Framework Networking and Communication, System.Net.WebException Top

Thanks for the reply but no luck with the suggestion of using

("http://cslibrary.stanford.edu"); No matter what website I use it wont work. I think I need to find out some information about the proxy details of the company that I work for or else I have no chance of getting this little piece of code to run.

that's why I suggested you try connecting to the server itself first. if there's a proxy, yes, you definitely need to code with the proxy in mind. typically it's not a transparent interaction.


 
 
Ameri





PostPosted: .NET Framework Networking and Communication, System.Net.WebException Top

Hi Michael,

We have a proxy webservice.cs file with the C# code that communicates to the webservices. I am not sure where to find the timeout property as I couldn't.

-Ameri


 
 
ace333





PostPosted: .NET Framework Networking and Communication, System.Net.WebException Top

I need to find out what the proxy is.

One more thing though - how would just using the beginning of the url help get around the proxy issue anyway



 
 
TaylorMichaelL





PostPosted: .NET Framework Networking and Communication, System.Net.WebException Top

The Timeout property is exposed off the web service just like the web methods are. The property is actually defined in SoapHttpClientProtocol from which your web service proxy derives.

Here is sample code for setting the timout of a web service before invoking it. The web service is the default version generated when you create a new web service project. I modified it to sleep 4 seconds so the following code will cause a timeout error to occur.

static void Main ( string[] args )
{
TestSvc.Service1 svc =
new ConsoleApplication1.TestSvc.Service1();
svc.Timeout =
2000;
svc.HelloWorld();
}

Michael Taylor - 2/20/07


 
 
AndrewBadera





PostPosted: .NET Framework Networking and Communication, System.Net.WebException Top

I need to find out what the proxy is.

One more thing though - how would just using the beginning of the url help get around the proxy issue anyway

it wouldn't, but it would help you debug your process. some request clients only like certain types of resources I believe, connecting to just the base url would establish whether or not you had connectivity to begin with, and eliminate some of the other possible variables that could be an issue.


 
 
ace333





PostPosted: .NET Framework Networking and Communication, System.Net.WebException Top

This is where I am now !!! - I know the proxy but I am still struggling

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Net;
using System.IO;

namespace DownLoadTime
{
/// <summary>
/// Summary description for WebForm2.
/// </summary>
public class WebForm2 : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{

try
{
#region
Uri uri = new Uri("http://torus.cbse.uab.edu/walter/unix_for_beginners.rtf_MRW.pdf");//N
//Uri uri = new Uri("http://www.google.com"); //Y
//Uri uri = new Uri("http://www.comp.glam.ac.uk/staff/ajcblyth/Research/Docs/Intro%20to%20Unix.doc");N
//Uri uri = new Uri("http://www.site.uottawa.ca/~oren/SCS_OC-MISS/lib-books-proceedings-2006-04-08.xls");N
//Uri uri = new Uri("http://news.bbc.co.uk/");//Y
//Uri uri = new Uri("http://news.bbc.co.uk/sport/");//N
//Uri uri = new Uri("http://www.bbc.co.uk/weather/");//N
//Uri uri = new Uri("http://localhost/DownLoadTime/WebForm1.aspx");//Y
#endregion


WebProxy myProxy = new WebProxy();
string proxyAddress;
proxyAddress = "http://xxx.xxx.net";
Uri newUri=new Uri(proxyAddress);
myProxy.Address=newUri;



WebRequest wr = HttpWebRequest.Create(uri);
wr.Proxy = myProxy;
wr.Credentials = CredentialCache.DefaultCredentials;
Stream stream = wr.GetResponse().GetResponseStream();
DateTime BeforeDlTime = DateTime.Now;
while(stream.ReadByte() != -1)
{
Response.Write(stream.ReadByte()+"<br/>");
}
TimeSpan Duration = DateTime.Now - BeforeDlTime;


Response.Write("Duration (Milliseconds)" +Duration.TotalMilliseconds.ToString()+"<br /><br />");
Response.Write("Duration (Minutes)" +Duration.TotalMinutes);
}

catch(WebException WebEx)
{
Response.Write("Web Exception Error "+"<br />");
Response.Write(WebEx.ToString());
}

catch(Exception GenEx)
{
Response.Write("General Error "+"<br />");
Response.Write(GenEx.ToString());
}

finally
{

}


}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
The above code produces the following error, I've spend hours and hours on this error, help would be much appreciated.
+ Yes the url does work in a browser.
+ Y:indicates the urls that work.
+ N:indicates the urls that don't work.

Web Exception Error
System.Net.WebException: The remote server returned an error: (404) Not Found. at System.Net.HttpWebRequest.CheckFinalStatus()
at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult) at System.Net.HttpWebRequest.GetResponse() at
DownLoadTime.WebForm2.Page_Load(Object sender, EventArgs e) in c:\inetpub\wwwroot\downloadtime\webform2.aspx.cs:line 50



 
 
MGillanders





PostPosted: .NET Framework Networking and Communication, System.Net.WebException Top

A while back when I was coding a similar app, I was having similar problems i.e "The underlying connection was closed". I seem to remember setting httpWebRequest.Proxy = Nothing to bypass the proxy settings to get it to work.
 
 
ace333





PostPosted: .NET Framework Networking and Communication, System.Net.WebException Top

Web Exception Error
System.Net.WebException: The remote server returned an error: (404) Not Found. at System.Net.HttpWebRequest.CheckFinalStatus()
at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult) at System.Net.HttpWebRequest.GetResponse() at
DownLoadTime.WebForm2.Page_Load(Object sender, EventArgs e) in

is the error I was getting when I set the value for the proxy when I found out what it was. The format of the proxy is http://xxx.proxy.net

I guess a 404 error refers to not being able to find a file. I'm behind a firewall / proxy at work. What I dont understand is the fact that I can get at

//Uri uri = new Uri("http://news.bbc.co.uk/");//Yes

Uri uri = new Uri("http://news.bbc.co.uk/sport/");//No -- returns the 404 page not found error