I am Currently Working on a project which i am facing a problem to check the HTTPS URL is correct or not. Now i have done validation for HTTP in c# now i want validation for HTTPS
My code for HTTP Validation.
HttpWebRequest re开发者_如何学Goquest = (HttpWebRequest)WebRequest.Create(url);
request.Proxy = WebProxy.GetDefaultProxy();
request.Proxy.Credentials = CredentialCache.DefaultCredentials;
// execute the request
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Response.Clear();
Response.Write(response.StatusDescription);
Response.End();
Anil
From MSDN
The Create method returns a descendant of the WebRequest class determined at run time as the closest registered match for requestUri.
For example, when a URI beginning with http:// or https:// is passed in requestUri, an HttpWebRequest is returned by Create.
If a URI beginning with ftp:// is passed instead, the Create method will return a FileWebRequest instance. If a URI beginning with file:// is passed instead, the Create method will return a FileWebRequest instance.
The pre-registered reserve types already registered include the following:
http://
https://
ftp://
file://
The .NET Framework includes support for the http://, https://, ftp://, and file:// URI schemes. Custom WebRequest descendants to handle other requests are registered with the RegisterPrefix method.
Full reference here
To resolve the error: "The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel" write this code before the request and the certificate should validate.
ServicePointManager.ServerCertificateValidationCallback +=
delegate(
object sender,
X509Certificate certificate,
X509Chain chain,
SslPolicyErrors sslPolicyErrors)
{
return true;
};
Ref : http://www.heatonresearch.com/articles/166/page2.html
try
{
Uri uri = new Uri("https://www.httprecipes.com/1/5/https.php");
WebRequest http = HttpWebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)http.GetResponse();
Stream stream = response.GetResponseStream();
}
catch(UriFormatException e)
{
Console.WriteLine("Invalid URL");
}
catch(IOException e)
{
Console.WriteLine("Could not connect to URL");
}
精彩评论