|
玄机论坛-专业的C#交流论坛 交流QQ群: 16885911如有疑问,请加入官方群询问
您需要 登录 才可以下载或查看,没有帐号?关闭注册
x
HttpCode使用篇
使用HttpCode时,请先引用 System.Web
Framework 4.0 时需要引用 System.Web.Extensions
使用httpcode的 异步模式异步的优势: 无需处理多线程的问题。
使用HttpHelpers类
异步模式 Get请求
[mw_shl_code=csharp,true]
HttpHelpers http = new HttpHelpers();//请求发起对象
HttpItems item = new HttpItems(); //请求设置对象
System.Net.CookieContainer cc = new System.Net.CookieContainer();//自动处理cookie对象
item.URL = "bbs.msdn5.com";//请求地址
item.Container = cc;//初始化cookie
Action<HttpResults> ActionCallback = new Action<HttpResults>(GetCallBack);//完成后的回调地址
http.AsyncGetHtml(item, ActionCallback);//执行异步请求,结果在ActionCallback的回调函数(GetCallBack)中查看
/// <summary>
/// 结果回调方法
/// </summary>
/// <param name="result">请求结果对象</param>
void GetCallBack(HttpResults result)
{
string res = result.Html;//返回成功的html数据.
}
[/mw_shl_code]
异步模式 Post请求
[mw_shl_code=csharp,true]
HttpHelpers http = new HttpHelpers();//请求发起对象
HttpItems item = new HttpItems(); //请求设置对象
System.Net.CookieContainer cc = new System.Net.CookieContainer();//自动处理cookie对象
string PostD = "Post需要的数据"; //提交的数据 必须项
item.URL = "bbs.msdn5.com";//提交的url地址
//item.Referer = "Referer数据";//如果需要请填写
item.Method = "Post"; //设置请求方式为post 默认get
//item.IsAjax = true; Ajax标志,如果不需要则不填写
item.Postdata = PostD;//设置提交的数据
item.Container = cc; //自动维护cookie对象
Action<HttpResults> ActionCallback = new Action<HttpResults>(Post_AsyncCallBack);//成功后的回调函数
http.AsyncGetHtml(item, ActionCallback);//发起请求.结果将在ActionCallback 的回调函数(Post_AsyncCallBack)中
//请求成功后的回调函数
private void Post_AsyncCallBack(HttpResults hrt)
{
string res = hrt.Html;//返回成功的html数据.
}
[/mw_shl_code]
|
|