• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

采集/自动登录啊都可以用这两个方法实现asp.net

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
/// <summary>
/// 通过get方式发送xmlHttp请求,并获得响应数据
/// </summary>
/// <param name="Url">URL地址,参数直接写到后面,如:http://www.baidu.com/index.asp?id=7</param>
/// <param name="Encoding">请求和返回数据采用的编码方式,如 "gb2312" ,"utf-8"</param>
/// <returns></returns>
public static string SendXmlHttpWithGet(string Url, string Encoding, CookieContainer cookie)
{
    HttpWebRequest request;// = (HttpWebRequest)WebRequest.Create(Url);
    string ResponseHtml = "";
    try
    {
        request = (HttpWebRequest)WebRequest.Create(Url);
        request.CookieContainer = cookie;
        request.KeepAlive = false; //是否建立持久连接
        request.Timeout = 6000; //超时时间
        request.Method = "get"; //get方式提交
        request.ContentType = "application/x-www-form-urlencoded;charset=" + Encoding;
        request.AllowAutoRedirect = true; //是否跟随重定向
        request.MaximumAutomaticRedirections = 10; //重定向最大数
        request.AllowWriteStreamBuffering = false; //是否对发送数据进行缓冲处理
        request.AuthenticationLevel = System.Net.Security.AuthenticationLevel.MutualAuthRequested; //进行身份验证

        HttpWebResponse response = (HttpWebResponse)request.GetResponse(); //获得响应
        Stream responseStream = response.GetResponseStream(); //获得响应流
        StreamReader readStream = new StreamReader(responseStream, System.Text.Encoding.GetEncoding(Encoding)); //读取字节的方式读取流

        ResponseHtml = readStream.ReadToEnd(); //读完流
        responseStream.Close(); //关闭响应流
        readStream.Close();//关闭字节流
    }
    catch (Exception ex)
    {
        ResponseHtml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><data><success value=\"0\" msg=\"登录失败!" + ex.Message.ToString() + "\" /></data>";
    }
    finally { }
    return ResponseHtml;
}

/// <summary>
/// 通过post方式发送xmlHttp请求,并获得响应数据
/// </summary>
/// <param name="url">URL地址</param>
/// <param name="parms">需要处理的参数键值对</param>
/// <param name="encoding">请求和返回数据采用的编码方式,如 "gb2312" ,"utf-8"</param>
/// <returns></returns>
public static string SendXmlHttpWithPost(string url, Hashtable parms, string encoding, CookieContainer cookie)
{
    HttpWebRequest request ;//= (HttpWebRequest)WebRequest.Create(url);
    string ResponseHtml = "";
    try
    {
        request = (HttpWebRequest)WebRequest.Create(url);
        request.CookieContainer = cookie;
        ////传参数
        if (encoding == null) { encoding = "utf-8"; }
        Encoding myEncoding = Encoding.GetEncoding(encoding); //指定编码

        string parmsStr = "";
        if (parms != null)
        {
            foreach (DictionaryEntry item in parms)
            {
                parmsStr += "&" + HttpUtility.UrlEncode(item.Key.ToString(), myEncoding);
                parmsStr += "=" + HttpUtility.UrlEncode(item.Value.ToString(), myEncoding);
            }
            if (parmsStr.Length > 0)
            {
                parmsStr = parmsStr.Substring(1, parmsStr.Length - 1); //把第一个"&"删了
            }
        }
        byte[] postBytes = myEncoding.GetBytes(parmsStr);

        request.Timeout = 60000; //超时时间
        request.Method = "post"; //采用post方法提交
        request.ContentType = "application/x-www-form-urlencoded;charset=" + myEncoding;
        request.ContentLength = postBytes.Length;
        Stream requestStream = request.GetRequestStream();
        requestStream.Write(postBytes, 0, postBytes.Length); //把参数写入请求流
        requestStream.Close();

        //获取响应
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();//获取响应
        Stream responseStream = response.GetResponseStream();//获取响应流
        StreamReader readStream = new StreamReader(responseStream, Encoding.GetEncoding("utf-8")); //采用StreamReader用指定编码读取响应流

        ResponseHtml = readStream.ReadToEnd(); //读完流
        responseStream.Close(); //关闭响应流
        readStream.Close();//关闭字节流
    }
    catch (Exception ex) 
    {
        ResponseHtml= "<?xml version=\"1.0\" encoding=\"utf-8\" ?><data><success value=\"0\" msg=\"登录失败!" + ex.Message.ToString() + "\" /></data>";
    }
    return ResponseHtml;

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
ASP.NET2.0的@Implements@Register@Assembly@PreviousPageType@MasterType指令发布时间:2022-07-10
下一篇:
【Asp.net】七大内置对象发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap