C#»ñÈ¡ÍøÒ³Ô´´úÂë(2009-09-28 23:01:18)תÔرêÇ©£ºit ·ÖÀࣺC#
·½·¨Ò»:
using System.Text;
using System.Net;
private string getHtml(string url)
{
WebClient myWebClient = new WebClient();
byte[] myDataBuffer = myWebClient.DownloadData (url);
return Encoding.Default.GetString(myDataBuffer);
}
·½·¨¶þ:
public string getHttp(string HttpUrl,string RefererUrl)
{
string html = "";
try
{
MSXML2.XMLHTTP Http = new MSXML2.XMLHTTPClass();
xmlhttp.open("GET", Url, false, null, null);
Http.open("GET",HttpUrl,false,null,null);
Http.setRequestHeader("Referer",RefererUrl);
//Http.setRequestHeader("Referer",RefererUrl);
Http.setRequestHeader("Content-Type", "text/html;charset=gb2312");
Http.send("");
html = Encoding.Default.GetString((byte[])sponseBody);
Http = null;
}
catch
{
}
return html;
}
public bool getweb(string strURL,out string buf)
{
buf="";
try
{
//Uri url=new Uri(strURL,false);
HttpWebRequest request;
request = (HttpWebRequest)WebRequest.Create(strURL);
request.Method="POST"; //PostÇëÇó·½Ê½
request.ContentType="text/html;charset=gb2312"; //ÄÚÈÝÀàÐÍ
string paraUrlCoded = System.Web.HttpUtility.UrlEncode(""); //²ÎÊý¾¹ýURL±àÂë
byte[] payload;
payload = System.Text.Encoding.GetEncoding("GB2312").GetBytes(paraUrlCoded); //½«URL±àÂëºóµÄ×Ö·û´®×ª»¯Îª×Ö½Ú
request.ContentLength = payload.Length; //ÉèÖÃÇëÇóµÄContentLength
Stream writer = request.GetRequestStream(); //»ñµÃÇëÇóÁ÷
writer.Write(payload,0,payload.Length); //½«ÇëÇó²ÎÊýдÈëÁ÷
writer.Close(); //¹Ø±ÕÇëÇóÁ÷
HttpWebResponse response;
response = (HttpWebResponse)request.GetResponse(); //»ñµÃÏìÓ¦Á÷
Stream s;
s = response.GetResponseStream();
StreamReader objReader = new StreamReader(s,System.Text.Encoding.GetEncoding("
GB2312"));
string HTML = "";
string sLine ="";
int i = 0;
while (sLine!=null)
{
i++;
sLine = objReader.ReadLine();
if (sLine!=null)
HTML += sLine;
}
//HTML = HTML.Replace("<","<");
//HTML = HTML.Replace(">",">");
buf=HTML;
return true;
}
catch (Exception x)
{
buf=x.Message.ToString();
return false;
}
}
´øCookie£º
CookieContainer cc = new CookieContainer();
public bool getweb(string strURL,out string buf)
{
buf="";
try
{
HttpWebRequest request;
request = (HttpWebRequest)WebRequest.Create(strURL);
request.Method="POST"; //PostÇëÇó·½Ê½
request.ContentType="text/html;charset=gb2312"; //ÄÚÈÝÀàÐÍ
string paraUrlCoded = System.Web.HttpUtility.UrlEncode(""); //²ÎÊý¾¹ýURL±àÂë
byte[] payload;
payload = System.Text.Encoding.GetEncoding("GB2312").GetBytes(paraUrlCoded); //½«URL±àÂëºóµÄ×Ö·û´®×ª»¯Îª×Ö½Ú
request.ContentLength = payload.Length; //ÉèÖÃÇëÇóµÄContentLength
Stream writer = request.GetRequestStream(); //»ñµÃÇëÇóÁ÷
writer.Write(payload,0,payload.Length); //½«ÇëÇó²ÎÊýдÈëÁ÷
writer.Close(); //¹Ø±ÕÇëÇóÁ÷
HttpWebResponse response;
response = (HttpWebResponse)request.GetResponse(); //»ñµÃÏìÓ¦Á÷
Stream s;
s = response.GetResponseStream();
StreamReader objReader = new StreamReader(s,System.Text.Encoding.GetEncoding("GB2312"));
string HTML = "";
旗舰28源码 string sLine ="";
int i = 0;
while (sLine!=null)
{
i++;
sLine = objReader.ReadLine();
if (sLine!=null)
HTML += sLine;
}
buf=HTML;
return true;
}
catch (Exception x)
{
buf=x.Message.ToString();
return false;
}
}
public bool getweb(string strURL,out string buf,string postData)
{
buf="";
try
{
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = encoding.GetBytes(postData);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strURL);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
Stream newStream = request.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();
request.CookieContainer = cc;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
cc.Add(response.Cookies);
Stream stream = response.GetResponseStream();
string sHtml = new StreamReader(stream, System.Text.Encoding.Default).ReadToEnd();
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论