Click here to Skip to main content
15,908,675 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace ishant
{
  
class WebFetch
{
	static void Main(string[] args)
	{
		// used to build entire input
		StringBuilder sb  = new StringBuilder();

		// used on each read operation
		byte[]        buf = new byte[8192];

		// prepare the web page we will be asking for
		HttpWebRequest  request  = (HttpWebRequest)
        
			WebRequest.Create("http://www.google.com");
       
                  //GETTING ERROR CODE 407!
		// execute the request
		HttpWebResponse response = (HttpWebResponse)
			request.GetResponse();

		// we will read data via the response stream
		Stream resStream = response.GetResponseStream();

		string tempString = null;
		int    count      = 0;

		do
		{
			// fill the buffer with data
			count = resStream.Read(buf, 0, buf.Length);

			// make sure we read some data
			if (count != 0)
			{
				// translate from bytes to ASCII text
				tempString = Encoding.ASCII.GetString(buf, 0, count);

				// continue building the string
				sb.Append(tempString);
			}
		}
		while (count > 0); // any more data to read?

		// print out page source
		Console.WriteLine(sb.ToString());
	}
}

        
}
Posted
Updated 7-Jul-10 22:31pm
v2
Comments
R. Giskard Reventlov 8-Jul-10 4:21am    
Is there a question in there, somewhere?
ishant7890 8-Jul-10 4:23am    
ya in bold,the thing is m using this code to access http,but its giving error code 407,proxy authentication.I know the system password and Id but how to supply it in the code?

1 solution

Proxy authentication required, so get the default IE settings you use and assign them to your WebRequest

C#
IWebProxy proxy = HttpWebRequest.DefaultWebProxy;
proxy.Credentials = CredentialCache.DefaultCredentials;

HttpWebRequest  request  = (HttpWebRequest)WebRequest.Create("http://www.google.com");
request.Proxy = proxy;

HttpWebResponse response = (HttpWebResponse)request.GetResponse();


* edit *

If you don't have default credentials on the machine that will be running the code, then you will need to supply some custom credentials

e.g

C#
IWebProxy proxy = HttpWebRequest.DefaultWebProxy;
NetworkCredential credentials = new NetworkCredential("UserName", "Password");
proxy.Credentials = credentials;

HttpWebRequest  request  = (HttpWebRequest)WebRequest.Create("http://www.google.com");
request.Proxy = proxy;

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
 
Share this answer
 
v3
Comments
ishant7890 8-Jul-10 5:59am    
but there are no default credentials,its a company and every time u open a browswer u have to supply user id n password,thats wat i want to supply in code.
Dylan Morley 8-Jul-10 6:18am    
OK, see updated answer. You'll need to create a NetworkCredential object to use with the proxy. Just supply user name + password that has appropriate access.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900