using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Net;
using System.IO;
/*
* HtmlDocument is class helping to obtain HTML source code from URL that we provide
* Input : URL we want to perform navigation testing
* Output : Scenario obtianed based on URL proximity
* Methods :
* Form1() : Initialization of data
* btn_Html_Click() : TO start the navigation testing operation
* getHTML() : Function to obtain source of URL specified
* Form1_Load() : Event Handler for form1 "load" event
* */
namespace HtmlDocument
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btn_Html_Click(object sender, EventArgs e)
{
/*
* Attributes:
* url - URL of web page we want to test
* pagesource - string variable which stores source code of webpage
* myWeb
*
*/
string url=string.Empty;
url=txt_url.Text;
if (url ==string.Empty)
{
MessageBox.Show("Enter url");
}
else
{
try
{
bool isUrlExist=true;
isUrlExist=IsUrlAvailable(url);
if(isUrlExist==true)
{
string pagesource = getHtml(url);
richTextBox1.Text = pagesource;
System.Diagnostics.Process.Start(url);
}
else{
MessageBox.Show("error");
System.Diagnostics.Process.Start(url);
Application.Run(new Form2());
}
}
catch (Exception ex)
{
MessageBox.Show("Unrecoverable error in"+url);
Application.Run(new Form2());
}
}
}
string getHtml(string url)
{
HttpWebRequest myWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
myWebRequest.Method = "GET";
// make request for web page
HttpWebResponse myWebResponse = (HttpWebResponse)myWebRequest.GetResponse();
StreamReader myWebSource = new StreamReader(myWebResponse.GetResponseStream());
string myPageSource = string.Empty;
myPageSource= myWebSource.ReadToEnd();
myWebResponse.Close();
return myPageSource;
}
public void showMessage(String msg)
{
MessageBox.Show(msg);
}
public bool IsUrlAvailable(string url)
{
try
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
using (HttpWebResponse rsp = (HttpWebResponse)req.GetResponse())
{
string msg;
MessageBox.Show("Code is "+ rsp.StatusCode);
if (rsp.StatusCode == HttpStatusCode.Continue)
{
msg="HTTP STAUS 100 : Continue indicates that the client can continue with its request.";
showMessage(msg);
return true;
}
if (rsp.StatusCode == HttpStatusCode.SwitchingProtocols)
{
msg="HTTP STATUS 101 : Protocol version or protocol is being changed.";
showMessage(msg);
return true;
}
if (rsp.StatusCode == HttpStatusCode.OK)
{
msg="HTTP STATUS 200 : OK indicates that the request succeeded and that the requested information is in the response";
showMessage(msg);
return true;
}
if (rsp.StatusCode == HttpStatusCode.Created)
{
msg="HTTP STATUS 201 : The request resulted in a new resource created before the response was sent.";
showMessage(msg);
return true;
}
if (rsp.StatusCode == HttpStatusCode.Accepted)
{
msg="HTTP STATUS 202 : Accepted indicates that the request has been accepted for further processing. ";
showMessage(msg);
return true;
}
if (rsp.StatusCode == HttpStatusCode.NonAuthoritativeInformation)
{
msg="HTTP STATUS 203 : the returned metainformation is from a cached copy instead of the origin server and therefore may be incorrect.";
showMessage(msg);
return true;
}
if (rsp.StatusCode == HttpStatusCode.NoContent)
{
msg="HTTP STATUS 204 : the request has been successfully processed and that the response is intentionally blank.";
showMessage(msg);
return true;
}
if (rsp.StatusCode == HttpStatusCode.ResetContent)
{
msg="HTTP STATUS 205 : The client should reset (not reload) the current resource.";
showMessage(msg);
return true;
}
if (rsp.StatusCode == HttpStatusCode.PartialContent)
{
msg="HTTP STATUS 206 : The response is a partial response as requested by a GET request that includes a byte range.";
showMessage(msg);
return true;
}
if (rsp.StatusCode == HttpStatusCode.MultipleChoices)
{
msg="HTTP STATUS 300 : MultipleChoices indicates that the requested information has multiple representations. The default action is to treat this status as a redirect and follow the contents of the Location header associated with this response.";
showMessage(msg);
return true;
}
if (rsp.StatusCode == HttpStatusCode.Ambiguous)
{
msg="HTTP STAUS 300 : Ambiguous indicates that the requested information has multiple representations. The default action is to treat this status as a redirect and follow the contents of the Location header associated with this response.";
showMessage(msg);
return true;
}
if (rsp.StatusCode == HttpStatusCode.Ambiguous)
{
msg="HTTP STATUS 301 : MovedPermanently indicates that the requested information has been moved to the URI specified in the Location header. The default action when this status is received is to follow the Location header associated with the response.";
showMessage(msg);
return true;
}
if (rsp.StatusCode == HttpStatusCode.Moved)
{
msg="HTTP STATUS 301 : Moved indicates that the requested information has been moved to the URI specified in the Location header. The default action when this status is received is to follow the Location header associated with the response. When the original request method was POST, the redirected request will use the GET method.";
showMessage(msg);
return true;
}
if (rsp.StatusCode == HttpStatusCode.Found)
{
msg="HTTP STATUS 302 : the requested information is located at the URI specified in the Location header. The default action when this status is received is to follow the Location header associated with the response. When the original request method was POST, the redirected request will use the GET method.";
showMessage(msg);
return true;
}
if (rsp.StatusCode == HttpStatusCode.Redirect)
{
msg="HTTP STATUS 302 : Redirect indicates that the requested information is located at the URI specified in the Location header. The default action when this status is received is to follow the Location header associated with the response. When the original request method was POST, the redirected request will use the GET method.";
showMessage(msg);
return true;
}
if (rsp.StatusCode == HttpStatusCode.SeeOther)
{
msg="HTTP STATUS 303 : SeeOther automatically redirects the client to the URI specified in the Location header as the result of a POST. The request to the resource specified by the Location header will be made with a GET.";
showMessage(msg);
return true;
}
if (rsp.StatusCode == HttpStatusCode.RedirectMethod)
{
msg="HTTP STATUS 303 : RedirectMethod automatically redirects the client to the URI specified in the Location header as the result of a POST. The request to the resource specified by the Location header will be made with a GET.";
showMessage(msg);
return true;
}
if (rsp.StatusCode == HttpStatusCode.NotModified)
{
msg="HTTP STATUS 304 : The client's cached copy is up to date. The contents of the resource are not transferred.";
showMessage(msg);
return true;
}
if (rsp.StatusCode == HttpStatusCode.UseProxy)
{
msg="HTTP STATUS 305 : The request should use the proxy server at the URI specified in the Location header.";
showMessage(msg);
return true;
}
if (rsp.StatusCode == HttpStatusCode.Unused)
{
msg="HTP STATUS 306 : Unused is a proposed extension to the HTTP/1.1 specification that is not fully specified. ";
showMessage(msg);
return true;
}
if (rsp.StatusCode == HttpStatusCode.TemporaryRedirect)
{
msg="HTTP STATUS 307 : the request information is located at the URI specified in the Location header. The default action when this status is received is to follow the Location header associated with the response. When the original request method was POST, the redirected request will also use the POST method.";
showMessage(msg);
return true;
}
if (rsp.StatusCode == HttpStatusCode.RedirectKeepVerb)
{
msg="HTTP STATUS 307 : the request information is located at the URI specified in the Location header. The default action when this status is received is to follow the Location header associated with the response. When the original request method was POST, the redirected request will also use the POST method.";
showMessage(msg);
return true;
}
if (rsp.StatusCode == HttpStatusCode.BadRequest)
{
msg="HTTP STATUS 400 : the request could not be understood by the server. BadRequest is sent when no other error is applicable, or if the exact error is unknown or does not have its own error code.";
showMessage(msg);
return false;
}
if (rsp.StatusCode == HttpStatusCode.Unauthorized)
{
msg="HTTP STATUS 401 : the requested resource requires authentication. The WWW-Authenticate header contains the details of how to perform the authentication ";
showMessage(msg);
return false;
}
if (rsp.StatusCode == HttpStatusCode.PaymentRequired)
{
msg=" HTTP STATUS 402 : PaymentRequired is reserved for future use";
showMessage(msg);
return false;
}
if (rsp.StatusCode == HttpStatusCode.Forbidden)
{
msg="HTTP STATUS 403 : the server refuses to fulfill the request";
showMessage(msg);
return false;
}
if (rsp.StatusCode == HttpStatusCode.NotFound)
{
msg="HTTP STATUS 404 : the requested resource does not exist on the server.";
showMessage(msg);
return false;
}
if (rsp.StatusCode == HttpStatusCode.MethodNotAllowed)
{
msg="HTTP STATUS 405 : the request method (POST or GET) is not allowed on the requested resource";
showMessage(msg);
return false;
}
if (rsp.StatusCode == HttpStatusCode.NotAcceptable)
{
msg="HTTP STATUS 406 : the client has indicated with Accept headers that it will not accept any of the available representations of the resource. ";
showMessage(msg);
return false;
}
if (rsp.StatusCode == HttpStatusCode.ProxyAuthenticationRequired)
{
msg="HTTP STATUS 407 : the requested proxy requires authentication. The Proxy-authenticate header contains the details of how to perform the authentication.";
showMessage(msg);
return false;
}
if (rsp.StatusCode == HttpStatusCode.RequestTimeout)
{
msg="HTTP STATUS 408 : RequestTimeout indicates that the client did not send a request within the time the server was expecting the request.";
showMessage(msg);
return false;
}
if (rsp.StatusCode == HttpStatusCode.Conflict)
{
msg="HTTP STATUS 409 : the request could not be carried out because of a conflict on the server.";
showMessage(msg);
return false;
}
if (rsp.StatusCode == HttpStatusCode.Gone)
{
msg="HTTP STATUS 410 : The requested resource is no longer available.";
showMessage(msg);
return false;
}
if (rsp.StatusCode == HttpStatusCode.LengthRequired)
{
msg="HTTP STATUS 411 : LengthRequired indicates that the required Content-length header is missing.";
showMessage(msg);
return false;
}
if (rsp.StatusCode == HttpStatusCode.PreconditionFailed)
{
msg="HTTP STATUS 412 : condition set for this request failed, and the request cannot be carried out. Conditions are set with conditional request headers like If-Match, If-None-Match, or If-Unmodified-Since.";
showMessage(msg);
return false;
}
if (rsp.StatusCode == HttpStatusCode.RequestEntityTooLarge)
{
msg="HTTP STATUS 413 : the request is too large for the server to process.";
showMessage(msg);
return false;
}
if (rsp.StatusCode == HttpStatusCode.RequestEntityTooLarge)
{
msg="HTTP STATUS 414: the URI is too long. ";
showMessage(msg);
return false;
}
if (rsp.StatusCode == HttpStatusCode.UnsupportedMediaType)
{
msg="HTTP STATUS 415 : the request is an unsupported type. ";
showMessage(msg);
return false;
}
if (rsp.StatusCode == HttpStatusCode.RequestedRangeNotSatisfiable)
{
msg="HTTP STATUS 416 : the range of data requested from the resource cannot be returned, either because the beginning of the range is before the beginning of the resource, or the end of the range is after the end of the resource.";
showMessage(msg);
return false;
}
if (rsp.StatusCode == HttpStatusCode.ExpectationFailed)
{
msg="HTTP STATUS 417 : an expectation given in an Expect header could not be met by the server.";
showMessage(msg);
return false;
}
if (rsp.StatusCode == HttpStatusCode.InternalServerError)
{
msg="HTTP STATUS 500 : a generic error has occurred on the server.";
showMessage(msg);
return false;
}
if (rsp.StatusCode == HttpStatusCode.NotImplemented)
{
msg="HTTP STATUS 501 : the server does not support the requested function. ";
showMessage(msg);
return false;
}
if (rsp.StatusCode == HttpStatusCode.BadGateway)
{
msg="HTTP STATUS 502 : An intermediate proxy server received a bad response from another proxy or the origin server.";
showMessage(msg);
return false;
}
if (rsp.StatusCode == HttpStatusCode.ServiceUnavailable)
{
msg="HTTP STATUS 503 : The server is temporarily unavailable, usually due to high load or maintenance.";
showMessage(msg);
return false;
}
if (rsp.StatusCode == HttpStatusCode.GatewayTimeout)
{
msg="HTTP STATUS 504 :GatewayTimeout indicates that an intermediate proxy server timed out while waiting for a response from another proxy or the origin server. ";
showMessage(msg);
return false;
}
if (rsp.StatusCode == HttpStatusCode.HttpVersionNotSupported)
{
msg="HTTP STATUS 505 : the requested HTTP version is not supported by the server.";
showMessage(msg);
return false;
}
}
}
catch (WebException)
{
// Eat it because all we want to do is return false
}
// Otherwise
return false;
}
private void Form1_Load(object sender, EventArgs e)
{
this.Text = "NavTester";
}
}
}
No comments:
Post a Comment