im trying to read a website and i keep getting the exception "underlying connection was closed"
...
WebClient w = new WebClient();
Stream s = w.openRead(addr);
... and yes addr has a 'http://' prefix
why am i getting this exception every time?
im trying to read a website and i keep getting the exception "underlying connection was closed"
...
WebClient w = new WebClient();
Stream s = w.openRead(addr);
... and yes addr has a 'http://' prefix
why am i getting this exception every time?
Short summary: the exception you saw often means your process could not complete an HTTP request because the network path was being intercepted or blocked. As found, switching to a proxy fixed it — which usually indicates the host machine or corporate network requires a proxy (sometimes with authentication). The two-line WebClient approach mentioned by works when no proxy is required; ’s WebRequest example is fine when you need explicit request/response control, but both need correct proxy settings in restricted networks.
Quick troubleshooting checklist
WebException, log Status, Response and any InnerException text.netsh winhttp show proxy.How to handle proxies from code
IWebProxy sysProxy = WebRequest.DefaultWebProxy ?? WebRequest.GetSystemWebProxy();
Uri target = new Uri(address);
Uri proxyForTarget = sysProxy.GetProxy(target);
bool usesProxy = proxyForTarget != target;
sysProxy.Credentials = CredentialCache.DefaultCredentials; Assign that proxy to your request/client or set sysProxy.Credentials so authenticated proxies succeed.
If you need to force a direct connection (for testing), bypass the proxy in code or in system settings. For modern HTTPS endpoints also check TLS versions (adjust ServicePointManager.SecurityProtocol if your runtime defaults are too old). If problems persist, run a network trace to confirm whether the proxy is rejecting connections or the remote server is closing them.
Jump to Post— Diamonddrake 397http://msdn.microsoft.com/en-us/library/781fwaz8.aspx
According to the 2 little lines you posted. it looks fine, this link is the usage, and that works fine on my machine. Must be something else. feel free to post …
Jump to Post— Mitja Bonca 5571st of all, you have to add a new namespace (using System.Net;)
Now I will show you a simple example of how to get the website`s text:List<string> list = new List<string>(); private void GetAllText() { WebRequest request = WebRequest.Create("http://www.daniweb.com"); WebResponse response = request.GetResponse(); using (StreamReader …
http://msdn.microsoft.com/en-us/library/781fwaz8.aspx
According to the 2 little lines you posted. it looks fine, this link is the usage, and that works fine on my machine. Must be something else. feel free to post more code.
1st of all, you have to add a new namespace (using System.Net;)
Now I will show you a simple example of how to get the website`s text:
List<string> list = new List<string>();
private void GetAllText()
{
WebRequest request = WebRequest.Create("http://www.daniweb.com");
WebResponse response = request.GetResponse();
using (StreamReader sr = new StreamReader(response.GetResponseStream()))
{
while (!sr.EndOfStream)
list.Add(sr.ReadLine()); //line by line it writes into a list<T>.
}
ShowingText();
} That is an odd example Mitja, you only need 2 lines of code for this, you defiantly don't want to separate all the strings into a generic list like that. And its unnecessary to handle the request and response like that. Just use your framework classes.
// Create web client.
WebClient client = new WebClient();
// Download string.
string value = client.DownloadString("http://www.daniweb.com/"); Ok, I put an example which I used ones - I had to seperate the lines of code.
my code compiles why would you assume that this is a compiler issue... its the webclient class thats not working correctly im trying to figure out why
solved was using a proxy lol
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.