[C#] 纯文本查看 复制代码
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)
{
}
这个方法在这里没有用到,大家可以做为参考
/// <summary>
/// 传入URL返回网页的html代码
/// </summary>
/// <param name="Url">URL</param>
/// <returns></returns>
public string GetUrltoHtml(string Url)
{
StringBuilder content = new StringBuilder();
try
{
// 与指定URL创建HTTP请求
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
request.KeepAlive = false;
// 获取对应HTTP请求的响应
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
// 获取响应流
Stream responseStream = response.GetResponseStream();
// 对接响应流(以"GBK"字符集)
StreamReader sReader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8"));
// 开始读取数据
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);
}
// 读取结束
sReader.Close();
}
catch (Exception)
{
content = new StringBuilder("Runtime Error");
}
return content.ToString();
}
/// <summary>
/// 好123查询,符合下列规则也可使用
/// 返回xml
/// 需要顺序的节点:
/// QueryResult(查询结果状态True,False)
/// Province(所属省份)
/// City(所属地区)
/// Corp(服务商)
/// Card(卡类型 GSM)
/// AreaCode(区号)
/// PostCode(邮编)
/// </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>河南</Province><City>郑州</City><AreaCode>0371</AreaCode><;PostCode>450000</PostCode><Corp>中国移动</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" };
}
}
//调用方法查询数据
private void button1_Click(object sender, EventArgs e)
{
foreach (string item in GetInfoByxml(" http://vip.showji.com/locating/?m={0}", txtMobile.Text.Trim()))
{
richTextBox1.Text += "__" + item;
}
}
}
}
[C#] 纯文本查看 复制代码
//回调验证证书问题
public bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
{ // 总是接受
return true;
}
[C#] 纯文本查看 复制代码
/// <summary>
/// 传入URL返回网页的html代码
/// </summary>
/// <param name="Url">URL</param>
/// <returns></returns>
public string GetUrltoHtml(string Url)
{
StringBuilder content = new StringBuilder();
try
{
// 与指定URL创建HTTP请求
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);//验证
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 = "*/*";
//创建证书文件
X509Certificate objx509 = new X509Certificate(Application.StartupPath + "\\123.cer");
//添加到请求里
request.ClientCertificates.Add(objx509);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
// 获取对应HTTP请求的响应
// 获取响应流
Stream responseStream = response.GetResponseStream();
// 对接响应流(以"GBK"字符集)
StreamReader sReader = new StreamReader(responseStream, Encoding.GetEncoding("GBK"));
// 开始读取数据
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);
}
// 读取结束
sReader.Close();
}
catch (Exception)
{
content = new StringBuilder("Runtime Error");
}
return content.ToString();
}