在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
/// <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;
|
请发表评论