Using system;
Using system. collections. generic;
Using system. componentmodel;
Using system. data;
Using system. drawing;
Using system. linq;
Using system. text;
Using system. windows. forms;
Using system.net;
Using system. io;
Using system. security. cryptography. x509certificates;
Using system.net. security;
Using system. security. cryptography;
Using system. xml;
Namespace ccbtext
{
Public partial class form2: form
{
Public form2 ()
{
Initializecomponent ();
}
Private void form2_load (object sender, eventargs e)
{
}
This method is not used here and can be used as a reference.
/// <Summary>
/// Input the url to return the html code of the webpage
/// </Summary>
/// <Param name = "url"> url </param>
/// <Returns> </returns>
Public string geturltohtml (string url)
{
Stringbuilder content = new stringbuilder ();
Try
{
// Create an http request with the specified url
Httpwebrequest request = (httpwebrequest) webrequest. create (url );
Request. keepalive = false;
// Obtain the response of the corresponding http request
Httpwebresponse response = (httpwebresponse) request. getresponse ();
// Obtain the response stream
Stream responsestream = response. getresponsestream ();
// Connect to the response stream (in the gbk character set)
Streamreader sreader = new streamreader (responsestream, encoding. getencoding ("UTF-8 "));
// Start reading data
Char [] sreaderbuffer = new char [256];
Int count = sreader. read (sreaderbuffer, 0,256 );
While (count> 0)
{
String temps tutorial tr = new string (sreaderbuffer, 0, count );
Content. append (tempstr );
Count = sreader. read (sreaderbuffer, 0,256 );
}
// Read ended
Sreader. close ();
}
Catch (exception)
{
Content = new stringbuilder ("runtime error ");
}
Return content. tostring ();
}
/// <Summary>
/// Check whether the query meets the following rules.
/// Return xml
/// Nodes in sequence:
/// Queryresult (query result status: true or false)
/// Province (province)
/// City (region)
/// Corp (service provider)
/// Card (card type gsm)
/// Areacode (area code)
/// Postcode (zip code)
/// </Summary>
/// <Param name = "url"> </param>
/// <Param name = "mobilenum"> </param>
/// <Returns> </returns>
Public static string [] getinfobyxml (string url, string mobilenum)
{
Try
{
Xmldocument xml = new xmldocument ();
// Xml. loadxml ("<? Xml version = '1. 0' encoding = 'utf-8'?> <Queryresponse xmlns = 'http: // api.showji.com/locating/'> <mobile> 15890636739 </mobile> <queryresult> true </queryresult> <province> Henan </province> <city> Zhengzhou </city> <areacode> 0371 </areacode> <postcode> 450000 </postcode> <corp> China Mobile </corp> <card> gsm </card> </queryresponse> ");
Xml. load (string. format (url, mobilenum ));
Xmlnamespacemanager xmlnm = new xmlnamespacemanager (xml. nametable );
Xmlnm. addnamespace ("content", "http://api.showji.com/locating ");
Xmlnodelist nodes = xml. selectnodes ("// content: queryresult | // content: mobile | // content: province | // content: city | // content: corp | // content: card | // content: areacode | // content: postcode ", xmlnm );
If (nodes. count = 8)
{
If ("true". equals (nodes [1]. innertext ))
{
Return new string [] {nodes [0]. innertext, nodes [2]. innertext + nodes [3]. innertext, nodes [6]. innertext + nodes [7]. innertext, nodes [4]. innertext, nodes [5]. innertext };
}
}
Return new string [] {"false "};
}
Catch
{
Return new string [] {"false "};
}
}
// Call a method to query data
Private void button#click (object sender, eventargs e)
{
Foreach (string item in getinfobyxml ("http://vip.showji.com/locating? M = {0} ", txtmobile. text. trim ()))
{
Richtextbox1.text + = "_" + item;
}
}
}
}
It is a test using winfrom. If you want to use asp tutorials. Net is the same. Copy my method to the cs code on your web page.
Now our analysis is over,
Here, I will add a call method for calling a website with a certificate to dakong.
Because all the files with the certificate are to be verified, we will directly ask him to perform local callback verification here. In this case, we need to rewrite the method. Let's take a look at the callback method.
// Callback verification certificate problems
Public bool checkvalidationresult (object sender, x509certificate certificate, x509chain chain, sslpolicyerrors errors)
{// Always accept
Return true;
}
Others are quite simple. Just add a few lines of code to the above method geturltohtml (). The modified method
/// <Summary>
/// Input the url to return the html code of the webpage
/// </Summary>
/// <Param name = "url"> url </param>
/// <Returns> </returns>
Public string geturltohtml (string url)
{
Stringbuilder content = new stringbuilder ();
Try
{
// Create an http request with the specified url
Servicepointmanager. servercertificatevalidationcallback = new system.net. security. remotecertificatevalidationcallback (checkvalidationresult); // verify
Httpwebrequest request = (httpwebrequest) webrequest. create (url );
Request. keepalive = false;
Request. useragent = "mozilla/4.0 (compatible; msie 7.0; windows nt 6.1; trident/5.0; slcc2 ;. net clr 2.0.50727 ;. net clr 3.5.30729 ;. net clr 3.0.30729; media center pc 6.0; infopath.2; ms-rtc lm 8 ;. net4.0c ;. net4.0e )";
Request. method = "get ";
Request. accept = "*/*";
// Create a certificate file
X509certificate objx509 = new x509certificate (application. startuppath + "123.cer ");
// Add to request
Request. clientcertificates. add (objx509 );
Httpwebresponse response = (httpwebresponse) request. getresponse ();
// Obtain the response of the corresponding http request
// Obtain the response stream
Stream responsestream = response. getresponsestream ();
// Connect to the response stream (in the gbk character set)
Streamreader sreader = new streamreader (responsestream, encoding. getencoding ("gbk "));
// Start reading data
Char [] sreaderbuffer = new char [256];
Int count = sreader. read (sreaderbuffer, 0,256 );
While (count> 0)
{
String tempstr = new string (sreaderbuffer, 0, count );
Content. append (tempstr );
Count = sreader. read (sreaderbuffer, 0,256 );
}
// Read ended
Sreader. close ();
}
Catch (exception)
{
Content = new stringbuilder ("runtime error ");
}
Return content. tostring ();
}