欢迎光临

木子屋

标题: C#使用HttpWebRequest和HttpWebResponse获得网页源代码

作者: dnawo

日期: 2012-10-27 05:12 PM

分类: Web编程

内容: 1.HttpHelper类
复制内容到剪贴板 程序代码using System;
using System.IO;
using System.Net;
using System.Text;
namespace ConsoleApplication1
{
public static class HttpHelper
{
private static readonly Encoding DEFAULTENCODE = Encoding.UTF8;
/// <summary>
/// Http get
/// </summary>
/// <param name="url"></param>
/// <param name="data"></param>
/// <returns></returns>
public static string Get(string url, string data)
{
return Get(url, data, DEFAULTENCODE);
}
/// <summary>
/// Http get
/// </summary>
/// <param name="url"></param>
/// <param name="data"></param>
/// <param name="encoding"></param>
/// <returns></returns>
public static string Get(string url, string data, Encoding encoding)
{
return Get(url, data, encoding, new CookieContainer());
}
/// <summary>
/// Http get
/// </summary>
/// <param name="url"></param>
/// <param name="data"></param>
/// <param name="encoding"></param>
/// <param name="cookieContainer"></param>
/// <returns></returns>
public static string Get(string url, string data, Encoding encoding, CookieContainer cookieContainer)
{
url = string.Format("{0}?{1}", url, data);
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Method = "GET";
//request.UserAgent = "";
//request.Referer = "";
//request.Proxy = null; //不使用代理，防止监听
//request.Proxy = new WebProxy("14.17.29.112", 80);
request.CookieContainer = cookieContainer;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (StreamReader stream = new StreamReader(response.GetResponseStream(), encoding))
{
return stream.ReadToEnd();
}
}
/// <summary>
/// Http post
/// </summary>
/// <param name="url"></param>
/// <param name="data"></param>
/// <returns></returns>
public static string Post(string url, string data)
{
return Post(url, data, DEFAULTENCODE);
}
/// <summary>
/// Http post
/// </summary>
/// <param name="url"></param>
/// <param name="data"></param>
/// <param name="encoding"></param>
/// <returns></returns>
public static string Post(string url, string data, Encoding encoding)
{
return Post(url, data, encoding, new CookieContainer());
}
/// <summary>
/// Http post
/// </summary>
/// <param name="url"></param>
/// <param name="data"></param>
/// <param name="encoding"></param>
/// <param name="cookieContainer"></param>
/// <returns></returns>
public static string Post(string url, string data, Encoding encoding, CookieContainer cookieContainer)
{
byte[] bytes = encoding.GetBytes(data);
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = bytes.Length;
//request.UserAgent = "";
//request.Referer = "";
//request.Proxy = null; //不使用代理，防止监听
//request.Proxy = new WebProxy("14.17.29.112", 80);
request.CookieContainer = cookieContainer;
using (Stream stream = request.GetRequestStream())
{
stream.Write(bytes, 0, bytes.Length);
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (StreamReader stream = new StreamReader(response.GetResponseStream(), encoding))
{
return stream.ReadToEnd();
}
}
}
}
2.调用示例
复制内容到剪贴板 程序代码using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(HttpHelper.Get("http://www.mzwu.com/article.asp", "id=1&t=1000"));
Console.ReadKey();
}
}
}

+ 查看当前日志评论 (0)

登录

────────────

木子屋

PJBlog3 v3.2.9.518 Inside.

Processed In 1.227 ms

返回

标题: C#使用HttpWebRequest和HttpWebResponse获得网页源代码

你没有权限发表评论

────────────

木子屋

PJBlog3 v3.2.9.518 Inside.

Processed In 1.227 ms

返回

暂无评论

返回

────────────

木子屋

PJBlog3 v3.2.9.518 Inside.

Processed In 1.258 ms

返回