111,131
社区成员
发帖
与我相关
我的任务
分享private string GetHtmlCode(string url, Encoding encoding)
{
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.1) Web-Sniffer/1.0.24";
System.Net.WebResponse response = request.GetResponse();
System.IO.Stream resStream = response.GetResponseStream();
System.IO.StreamReader sr = new System.IO.StreamReader(resStream, encoding);
string html = (sr.ReadToEnd());
resStream.Close();
sr.Close();
return html;
}
//调用
string html = GetHtmlCode("http://www.boc.cn/sourcedb/whpj/", Encoding.UTF8); protected void Page_Load(object sender, EventArgs e)
{
string url = "http://www.boc.cn/cn/common/whpj.html";
string encodeType = "gb2312";
string err = "";
string postData2 = "";
Response.Write(WebClinetPost(url, postData2, encodeType,out err));
}
static string WebClinetPost(string url, string postData, string encodeType, out string err)
{
string uriString = url;
byte[] byteArray;
byte[] responseArray;
Encoding encoding = Encoding.GetEncoding(encodeType);
try
{
WebClient myWebClient = new WebClient();
WebHeaderCollection myWebHeaderCollection;
myWebClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
myWebHeaderCollection = myWebClient.Headers;
byteArray = encoding.GetBytes(postData);
responseArray = myWebClient.UploadData(uriString, "POST", byteArray);
err = string.Empty;
return encoding.GetString(responseArray);
}
catch (Exception ex)
{
err = ex.Message;
return err;
}
}protected void Page_Load(object sender, EventArgs e)
{
string url = "http://www.boc.cn/cn/common/whpj.html";
string encodeType = "gb2312";
string err = "";
Response.Write(WebClinetPost(url, postData2, encodeType, out err));
}
static string WebClinetPost(string url, string postData, string encodeType, out string err)
{
string uriString = url;
byte[] byteArray;
byte[] responseArray;
Encoding encoding = Encoding.GetEncoding(encodeType);
try
{
WebClient myWebClient = new WebClient();
WebHeaderCollection myWebHeaderCollection;
myWebClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
myWebHeaderCollection = myWebClient.Headers;
byteArray = encoding.GetBytes(postData);
responseArray = myWebClient.UploadData(uriString, "POST", byteArray);
err = string.Empty;
return encoding.GetString(responseArray);
}
catch (Exception ex)
{
err = ex.Message;
return err;
}
}

protected void Page_Load(object sender, EventArgs e)
{
string url = "http://www.boc.cn/cn/common/whpj.html";
string encodeType = "gb2312";
string err = "";
Response.Write(WebClinetPost(url, postData2, encodeType, out err));
}
static string WebClinetPost(string url, string postData, string encodeType, out string err)
{
string uriString = url;
byte[] byteArray;
byte[] responseArray;
Encoding encoding = Encoding.GetEncoding(encodeType);
try
{
WebClient myWebClient = new WebClient();
WebHeaderCollection myWebHeaderCollection;
myWebClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
myWebHeaderCollection = myWebClient.Headers;
byteArray = encoding.GetBytes(postData);
responseArray = myWebClient.UploadData(uriString, "POST", byteArray);
err = string.Empty;
return encoding.GetString(responseArray);
}
catch (Exception ex)
{
err = ex.Message;
return err;
}
}
