在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
//postdata为数组的请求方式 public byte[] POST(string Url, byte[] byteRequest) { byte[] responsebody; HttpWebRequest httpWebRequest = null; HttpWebResponse httpWebResponse = null; try { //如果是发送HTTPS请求 if (Url.StartsWith("https", StringComparison.OrdinalIgnoreCase)) { ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(Url); httpWebRequest.ProtocolVersion = HttpVersion.Version10; } else { httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(Url);//创建连接请求 } httpWebRequest.Method = "POST"; if (cookieContainer != null) { httpWebRequest.CookieContainer = cookieContainer; } httpWebRequest.AllowAutoRedirect = AllowAutoRedirect;//【注意】这里有个时候在特殊情况下要设置为否,否则会造成cookie丢失 httpWebRequest.ContentType = ContentType; httpWebRequest.Accept = Accept; httpWebRequest.UserAgent = UserAgent; if (!string.IsNullOrEmpty(uuid)) { httpWebRequest.Headers.Add("seed:" + uuid + ""); } //Post请求数据,则写入传的PostData //byte[] byteRequest = Encoding.Default.GetBytes(PostData); httpWebRequest.ContentLength = byteRequest.Length; using (Stream stream = httpWebRequest.GetRequestStream()) { stream.Write(byteRequest, 0, byteRequest.Length); } httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();//开始获取响应流 Stream responseStream = httpWebResponse.GetResponseStream(); responsebody = StreamToBytes(responseStream); responseStream.Close(); httpWebRequest.Abort(); cookieContainer.Add(httpWebResponse.Cookies); cookieCollection.Add(httpWebResponse.Cookies); httpWebResponse.Close(); //到这里为止,所有的对象都要释放掉,以免内存像滚雪球一样 } catch (Exception ex) { responsebody = Encoding.Default.GetBytes(ex.Message + ex.Source); LogHelper.Log.Error("POST方式请求网页异常", ex); } return responsebody; }
//postdata为数组的请求方式 public byte[] POST(string Url, byte[] byteRequest) { byte[] responsebody; HttpWebRequest httpWebRequest = null; HttpWebResponse httpWebResponse = null; try { //如果是发送HTTPS请求 if (Url.StartsWith("https", StringComparison.OrdinalIgnoreCase)) { ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(Url); httpWebRequest.ProtocolVersion = HttpVersion.Version10; } else { httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(Url);//创建连接请求 } httpWebRequest.Method = "POST"; if (cookieContainer != null) { httpWebRequest.CookieContainer = cookieContainer; } httpWebRequest.AllowAutoRedirect = AllowAutoRedirect;//【注意】这里有个时候在特殊情况下要设置为否,否则会造成cookie丢失 httpWebRequest.ContentType = ContentType; httpWebRequest.Accept = Accept; httpWebRequest.UserAgent = UserAgent; if (!string.IsNullOrEmpty(uuid)) { httpWebRequest.Headers.Add("seed:" + uuid + ""); } //Post请求数据,则写入传的PostData //byte[] byteRequest = Encoding.Default.GetBytes(PostData); httpWebRequest.ContentLength = byteRequest.Length; using (Stream stream = httpWebRequest.GetRequestStream()) { stream.Write(byteRequest, 0, byteRequest.Length); } httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();//开始获取响应流 Stream responseStream = httpWebResponse.GetResponseStream(); responsebody = StreamToBytes(responseStream); responseStream.Close(); httpWebRequest.Abort(); cookieContainer.Add(httpWebResponse.Cookies); cookieCollection.Add(httpWebResponse.Cookies); httpWebResponse.Close(); //到这里为止,所有的对象都要释放掉,以免内存像滚雪球一样 } catch (Exception ex) { responsebody = Encoding.Default.GetBytes(ex.Message + ex.Source); LogHelper.Log.Error("POST方式请求网页异常", ex); } return responsebody; }
|
请发表评论