/ Published in: C#
                    
                                        
These static functions will execute an http GET and get the response as a string. The seconds function uses a user/password to login.
                
                            
                                Expand |
                                Embed | Plain Text
                            
                        
                        Copy this code and paste it in your HTML
/// <summary>
/// Get a response as a string, given a uri string.
/// </summary>
/// <param name="uriArg">Specifies a uri such as "http://www.google.com" or @"file://X:\dir\myFile.html"</param>
/// <returns>web response as a string.</returns>
/// <example>
/// try
/// {
/// string uri = "http://www.google.zzz"; // note bad uri with zzz to exercise exception.
/// string s = GetWebPageResponse( uri );
/// Console.WriteLine( s );
/// }
/// catch ( WebException ex )
/// {
/// // wex.Message could be something like: The remote server returned an error: (404) Not Found.
/// string s = string.Format( "Request: {0}\nResult: {1}", uri, ex.Message );
/// Console.WriteLine( s );
/// }
/// </example>
static string GetWebPageResponse(string uriArg)
{
Stream responseStream = WebRequest.Create(uriArg).GetResponse().GetResponseStream();
return reader.ReadToEnd();
}
/// <summary>
/// Similar to GetWebPageResponse(string uriArg), but uses a user/pw to log in.
/// </summary>
/// <param name="uriArg">e.g. "http://192.168.2.1"</param>
/// <param name="userArg">e.g. "root"</param>
/// <param name="pwArg">e.g. "admin"</param>
/// <returns>string containing the http response.</returns>
/// <example>
/// // Example to get a response with DHCP table from my LinkSys router.
/// string s = GetWebPageResponse( "http://192.168.2.1/DHCPTable.htm", "root", "admin" );
/// </example>
static string GetWebPageResponse(string uriArg, string userArg, string pwArg)
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri);
// See http://msdn.microsoft.com/en-us/library/system.directoryservices.protocols.authtype.aspx for list of types.
const string authType = "basic";
req.PreAuthenticate = true;
req.Credentials = creds.GetCredential(uri, authType);
Stream responseStream = req.GetResponse().GetResponseStream();
return reader.ReadToEnd();
}
Comments
 Subscribe to comments
                    Subscribe to comments
                
                