public async Task HttpHeader_Set_Success() { await LoopbackServer.CreateServerAsync(async (server, url) => { HttpWebRequest request = WebRequest.CreateHttp(url); request.Method = HttpMethod.Get.Method; Task<WebResponse> getResponse = request.GetResponseAsync(); DateTimeOffset utcNow = DateTimeOffset.UtcNow; await LoopbackServer.ReadRequestAndSendResponseAsync(server, $"HTTP/1.1 200 OK\r\n" + $"Date: {utcNow:R}\r\n" + "Content-Type: application/json;charset=UTF-8\r\n" + "Content-Length: 5\r\n" + "\r\n" + "12345"); using (WebResponse response = await getResponse) { HttpWebResponse httpResponse = (HttpWebResponse)response; Assert.Equal("UTF-8", httpResponse.CharacterSet); Assert.Equal(HttpStatusCode.OK, httpResponse.StatusCode); Assert.Equal("OK", httpResponse.StatusDescription); CookieCollection cookieCollection = new CookieCollection(); httpResponse.Cookies = cookieCollection; Assert.Equal(cookieCollection, httpResponse.Cookies); Assert.Equal(5,httpResponse.ContentLength); Assert.Equal(5, int.Parse(httpResponse.GetResponseHeader("Content-Length"))); } }); }
private static HttpWebResponse PostForm(string postUrl, string userAgent, string contentType, byte[] formData, CookieCollection cookie) { HttpWebRequest request = WebRequest.Create(postUrl) as HttpWebRequest; if (request == null) { throw new NullReferenceException("request is not a http request"); } request.Method = "POST"; request.ContentType = contentType; request.UserAgent = userAgent; if (cookie != null) { request.CookieContainer = new CookieContainer(); request.CookieContainer.Add(cookie); } else { request.CookieContainer = new CookieContainer(); } request.ContentLength = formData.Length; using (Stream stream = request.GetRequestStream()) { stream.Write(formData, 0, formData.Length); stream.Close(); } return (request.GetResponse() as HttpWebResponse); }
private static CookieContainer CreateCount11Container() { CookieContainer cc1 = new CookieContainer(); // Add(Cookie) cc1.Add(c1); cc1.Add(c2); cc1.Add(c3); cc1.Add(c4); // Add(CookieCollection) CookieCollection cc2 = new CookieCollection(); cc2.Add(c5); cc2.Add(c6); cc2.Add(c7); cc1.Add(cc2); // Add(Uri, Cookie) cc1.Add(u4, c8); cc1.Add(u4, c9); // Add(Uri, CookieCollection) cc2 = new CookieCollection(); cc2.Add(c10); cc2.Add(c11); cc1.Add(u5, cc2); return cc1; }
public static HttpWebResponse MultipartFormDataPost(string postUrl, string userAgent, Dictionary<string, object> postParameters, CookieCollection cookie) { string boundary = string.Format("----------{0:N}", Guid.NewGuid()); string contentType = "multipart/form-data; boundary=" + boundary; byte[] multipartFormData = GetMultipartFormData(postParameters, boundary); return PostForm(postUrl, userAgent, contentType, multipartFormData, cookie); }
private static CookieCollection CreateCookieCollection2() { CookieCollection cc = new CookieCollection(); cc.Add(CreateCookieCollection1()); return cc; }
public static void AddCookieCollection_Success() { CookieContainer cc = new CookieContainer(); CookieCollection cookieCollection = new CookieCollection(); cookieCollection.Add(new Cookie("name3", "value","/",".contoso.com")); cc.Add(cookieCollection); Assert.Equal(1, cc.Count); }
/// <summary> /// Initializes a new instance of the <see cref="Response"/> class. /// </summary> public Response() { this.cookies = new CookieCollection(); this.headers = new HeaderCollection(); this.headersSent = false; this.contentType = MimeType.Default; this.outputBuffer = new List<byte>(); this.sent = 0; this.status = StatusCode.Http500InternalServerError; this.useChunkedTransferEncoding = false; this.useCompression = false; }
private static Cookie c5 = new Cookie("name3", "value", "path", "different-domain"); //Same name, different domain private static CookieCollection CreateCookieCollection1() { CookieCollection cc = new CookieCollection(); cc.Add(c1); cc.Add(c2); cc.Add(c3); cc.Add(c4); cc.Add(c5); return cc; }
public crifanLib() { //http related //set max enough to avoid http request is used out -> avoid dead while get response System.Net.ServicePointManager.DefaultConnectionLimit = 200; curCookies = new CookieCollection(); // init const cookie keys foreach (string key in cookieFieldArr) { cookieFieldList.Add(key); } //init for calc time calcTimeList = new Dictionary<string, DateTime>(); }
internal HttpListenerWebSocketContext( Uri requestUri, NameValueCollection headers, CookieCollection cookieCollection, IPrincipal user, bool isAuthenticated, bool isLocal, bool isSecureConnection, string origin, IEnumerable<string> secWebSocketProtocols, string secWebSocketVersion, string secWebSocketKey, WebSocket webSocket) { Debug.Assert(requestUri != null, "requestUri shouldn't be null"); Debug.Assert(headers != null, "headers shouldn't be null"); Debug.Assert(cookieCollection != null, "cookieCollection shouldn't be null"); Debug.Assert(secWebSocketProtocols != null, "secWebSocketProtocols shouldn't be null"); Debug.Assert(webSocket != null, "webSocket shouldn't be null"); _cookieCollection = new CookieCollection(); _cookieCollection.Add(cookieCollection); _headers = new NameValueCollection(headers); _user = CopyPrincipal(user); _requestUri = requestUri; _isAuthenticated = isAuthenticated; _isLocal = isLocal; _isSecureConnection = isSecureConnection; _origin = origin; _secWebSocketProtocols = secWebSocketProtocols; _secWebSocketVersion = secWebSocketVersion; _secWebSocketKey = secWebSocketKey; _webSocket = webSocket; }
/// <summary> /// 发送POST请求 /// </summary> /// <param name="url">请求的URL地址</param> /// <param name="content">请求内容</param> /// <param name="encodingName">编码格式</param> /// <param name="timeout">超时时间 以毫秒为单位</param> /// <param name="cookies">请求时,附带的Cookie值</param> /// <param name="headers">请求时,附带的Header值</param> /// <param name="contentType">Content-Type Header标头</param> public static string SendPostRequestCore(string url, string content, string encodingName, int?timeout, CookieCollection cookies, NameValueCollection headers, string contentType) { HttpWebRequest request = null; try { // 创建HTTP请求 request = CreateHttpRequest(url, "POST", timeout); // 写入Cookie WriteRequestCookie(request, cookies); // 写入header值 WriteRequestHeader(request, headers); if (!string.IsNullOrWhiteSpace(contentType)) { request.ContentType = contentType; } //这个在Post的时候,一定要加上,如果服务器返回错误,他还会继续再去请求,不会使用之前的错误数据,做返回数据 //request.ServicePoint.Expect100Continue = false; // 写入POST数据 WriteRequestContent(request, content, encodingName); // 接收HTTP响应 return(ReceiveResponse(request, encodingName)); } catch (Exception ex) { var result = ex.Message; if (request != null) { request.Abort(); } return(result); } finally { if (request != null) { request.Abort(); } } }
//add singel cookie to cookies, default no overwrite domain public void addCookieToCookies(Cookie toAdd, ref CookieCollection cookies) { addCookieToCookies(toAdd, ref cookies, false); }
/*********************************************************************/ /* HTTP */ /*********************************************************************/ /* * Note: currently support auto handle cookies * currently only support single caller -> multiple caller of these functions will cause cookies accumulated * you can clear previous cookies to avoid unexpected result by call clearCurCookies */ public void clearCurCookies() { if (curCookies != null) { curCookies = null; curCookies = new CookieCollection(); } }
/// <summary> /// 创建GET方式的HTTP请求 /// </summary> public static HttpWebResponse CreateGetHttpResponse(string url, int timeout, string userAgent, CookieCollection cookies) { HttpWebRequest request = null; if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase)) { //对服务端证书进行有效性校验(非第三方权威机构颁发的证书,如自己生成的,不进行验证,这里返回true) ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); request = WebRequest.Create(url) as HttpWebRequest; request.ProtocolVersion = HttpVersion.Version10; //http版本,默认是1.1,这里设置为1.0 } else { request = WebRequest.Create(url) as HttpWebRequest; } request.Method = "GET"; //设置代理UserAgent和超时 //request.UserAgent = userAgent; //request.Timeout = timeout; if (cookies != null) { request.CookieContainer = new CookieContainer(); request.CookieContainer.Add(cookies); } return(request.GetResponse() as HttpWebResponse); }
public static string _post_with_cookies(string url, Dictionary <string, string> form_data, CookieCollection cookies) { string retString = ""; foreach (KeyValuePair <string, string> fd in form_data) { retString += "&" + HttpUtility.UrlEncode(fd.Key) + "=" + HttpUtility.UrlEncode(fd.Value); } retString = retString.Substring(1); byte[] bs = Encoding.UTF8.GetBytes(retString); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; request.CookieContainer = ToContainer(cookies); request.ContentLength = bs.Length; request.UserAgent = USER_AGENT; //提交请求数据 Stream reqStream = request.GetRequestStream(); reqStream.Write(bs, 0, bs.Length); reqStream.Close(); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream myResponseStream = response.GetResponseStream(); StreamReader streamReader = new StreamReader(myResponseStream); retString = streamReader.ReadToEnd(); streamReader.Close(); myResponseStream.Close(); return(retString); }
/// <summary> /// 发送POST请求 /// </summary> /// <param name="url">请求的URL地址</param> /// <param name="content">请求内容</param> /// <param name="encodingName">编码格式</param> /// <param name="timeout">超时时间 以毫秒为单位</param> /// <param name="cookies">请求时,附带的Cookie值</param> /// <param name="headers">请求时,附带的Header值</param> public static string SendPostRequestCore(string url, string content, string encodingName, int?timeout, CookieCollection cookies, NameValueCollection headers) { return(SendPostRequestCore(url, content, encodingName, timeout, cookies, headers, "application/json;charset=UTF-8")); }
public void Add(System.Uri uri, CookieCollection cookies) {}
/// <summary> /// http请求 /// </summary> /// <param name="url"></param> /// <param name="method">POST,GET</param> /// <param name="headers">http的头部,直接拷贝谷歌请求的头部即可</param> /// <param name="content">content,每个key,value 都要UrlEncode才行</param> /// <param name="contentEncode">content的编码</param> /// <param name="proxyUrl">代理url</param> /// <returns></returns> public string http(string url, string method, string headers, string content, Encoding contentEncode, string proxyUrl) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = method; if (method.Equals("GET", StringComparison.InvariantCultureIgnoreCase)) { request.MaximumAutomaticRedirections = 100; request.AllowAutoRedirect = false; } fillHeaders(request, headers); fillProxy(request, proxyUrl); #region 添加Post 参数 if (contentEncode == null) { contentEncode = Encoding.UTF8; } if (!string.IsNullOrWhiteSpace(content)) { byte[] data = contentEncode.GetBytes(content); request.ContentLength = data.Length; using (Stream reqStream = request.GetRequestStream()) { reqStream.Write(data, 0, data.Length); reqStream.Close(); } } #endregion HttpWebResponse response = null; System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); try { sw.Start(); response = (HttpWebResponse)request.GetResponse(); sw.Stop(); AvgResponseMilliseconds = sw.ElapsedMilliseconds; CookieCollection cc = new CookieCollection(); string cookieString = response.Headers[HttpResponseHeader.SetCookie]; if (!string.IsNullOrWhiteSpace(cookieString)) { var spilit = cookieString.Split(';'); foreach (string item in spilit) { var kv = item.Split('='); if (kv.Length == 2) { cc.Add(new Cookie(kv[0].Trim().ToString().Replace(",", "|*|"), kv[1].Trim())); } } } trackCookies(cc); } catch (Exception ex) { sw.Stop(); AvgResponseMilliseconds = sw.ElapsedMilliseconds; return(ex.Message); } string result = getResponseBody(response); return(result); }
/// <summary> /// 填充头 /// </summary> /// <param name="request"></param> /// <param name="headers"></param> private void fillHeaders(HttpWebRequest request, string headers, bool isPrint = false) { if (request == null) { return; } if (string.IsNullOrWhiteSpace(headers)) { return; } string[] hsplit = headers.Split(new String[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries); foreach (string item in hsplit) { string[] kv = item.Split(':'); string key = kv[0].Trim(); string value = string.Join(":", kv.Skip(1)).Trim(); if (!UNCHANGEHEADS.Contains(key)) { request.Headers.Add(key, value); } else { #region 设置http头 switch (key) { case "Accept": { request.Accept = value; break; } case "Host": { request.Host = value; break; } case "Connection": { if (value == "keep-alive") { request.KeepAlive = true; } else { request.KeepAlive = false; //just test } break; } case "Content-Type": { request.ContentType = value; break; } case "User-Agent": { request.UserAgent = value; break; } case "Referer": { request.Referer = value; break; } case "Content-Length": { request.ContentLength = Convert.ToInt64(value); break; } case "Expect": { request.Expect = value; break; } case "If-Modified-Since": { request.IfModifiedSince = Convert.ToDateTime(value); break; } default: break; } #endregion } } CookieCollection cc = new CookieCollection(); string cookieString = request.Headers[HttpRequestHeader.Cookie]; if (!string.IsNullOrWhiteSpace(cookieString)) { var spilit = cookieString.Split(';'); foreach (string item in spilit) { var kv = item.Split('='); if (kv.Length == 2) { cc.Add(new Cookie(kv[0].Trim(), kv[1].Trim())); } } } trackCookies(cc); if (!isTrackCookies) { request.Headers[HttpRequestHeader.Cookie] = ""; } else { request.Headers[HttpRequestHeader.Cookie] = getCookieStr(); } #region 打印头 if (isPrint) { for (int i = 0; i < request.Headers.AllKeys.Length; i++) { string key = request.Headers.AllKeys[i]; System.Console.WriteLine(key + ":" + request.Headers[key]); } } #endregion }
public string Get(string url, Encoding responseEncoding, int?timeout = 300, string userAgent = "", CookieCollection cookies = null, string Referer = "", Dictionary <string, string> headers = null) { HttpWebResponse response = CreateGetHttpResponse(url, timeout, userAgent, cookies, Referer, headers); try { using (StreamReader reader = new StreamReader(response.GetResponseStream(), responseEncoding)) { return(reader.ReadToEnd()); } } catch (Exception) { return(null); } }
public byte[] GetFile(string url, out Dictionary <string, string> header, int?timeout = 300, string userAgent = "", CookieCollection cookies = null, string Referer = "", Dictionary <string, string> headers = null) { HttpWebResponse response = CreateGetHttpResponse(url, timeout, userAgent, cookies, Referer, headers); header = new Dictionary <string, string>(); foreach (string key in response.Headers.AllKeys) { header.Add(key, response.Headers[key]); } try { System.IO.Stream st = response.GetResponseStream(); byte[] by = new byte[response.ContentLength]; st.Read(by, 0, by.Length); st.Close(); return(by); } catch (Exception) { return(null); } }
/// <summary> /// 创建GET方式的HTTP请求 /// </summary> /// <param name="url">请求的URL</param> /// <param name="timeout">请求的超时时间</param> /// <param name="userAgent">请求的客户端浏览器信息,可以为空</param> /// <param name="cookies">随同HTTP请求发送的Cookie信息,如果不需要身份验证可以为空</param> /// <returns></returns> public HttpWebResponse CreateGetHttpResponse(string url, int?timeout = 300, string userAgent = "", CookieCollection cookies = null, string Referer = "", Dictionary <string, string> headers = null) { if (Debug) { Console.Write("Start Get Url:{0} ", url); } if (string.IsNullOrEmpty(url)) { throw new ArgumentNullException("url"); } HttpWebRequest request; if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase)) { ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); request = WebRequest.Create(url) as HttpWebRequest; request.ProtocolVersion = HttpVersion.Version10; } else { request = WebRequest.Create(url) as HttpWebRequest; } request.Method = "GET"; request.Headers["Pragma"] = "no-cache"; request.Accept = "text/html, application/xhtml+xml, */*"; request.Headers["Accept-Language"] = "en-US,en;q=0.5"; request.ContentType = "application/x-www-form-urlencoded"; request.UserAgent = DefaultUserAgent; request.Referer = Referer; if (headers != null) { foreach (var header in headers) { request.Headers.Add(header.Key, header.Value); } } if (!string.IsNullOrEmpty(userAgent)) { request.UserAgent = userAgent; } if (timeout.HasValue) { request.Timeout = timeout.Value * 1000; } if (cookies != null) { request.CookieContainer = new CookieContainer(); request.CookieContainer.Add(cookies); } else { request.CookieContainer = new CookieContainer(); request.CookieContainer.Add(Cookies); } var v = request.GetResponse() as HttpWebResponse; Cookies.Add(request.CookieContainer.GetCookies(new Uri("http://" + new Uri(url).Host))); Cookies.Add(request.CookieContainer.GetCookies(new Uri("https://" + new Uri(url).Host))); Cookies.Add(v.Cookies); if (Debug) { Console.WriteLine("OK"); } return(v); }
/// <summary> /// 创建POST方式的HTTP请求 /// </summary> /// <param name="url">请求的URL</param> /// <param name="parameters">随同请求POST的参数名称及参数值字典</param> /// <param name="timeout">请求的超时时间</param> /// <param name="userAgent">请求的客户端浏览器信息,可以为空</param> /// <param name="requestEncoding">发送HTTP请求时所用的编码</param> /// <param name="cookies">随同HTTP请求发送的Cookie信息,如果不需要身份验证可以为空</param> /// <returns></returns> public HttpWebResponse CreatePostFileHttpResponse(string url, string filePath, int?timeout = 300, string userAgent = "", CookieCollection cookies = null, string Referer = "", Dictionary <string, string> headers = null) { if (Debug) { Console.Write("Start Post Url:{0} ", url); } if (string.IsNullOrEmpty(url)) { throw new ArgumentNullException("url"); } HttpWebRequest request = null; //如果是发送HTTPS请求 if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase)) { ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); request = WebRequest.Create(url) as HttpWebRequest; request.ProtocolVersion = HttpVersion.Version10; } else { request = WebRequest.Create(url) as HttpWebRequest; } request.Method = "POST"; request.Accept = "text/html, application/xhtml+xml, application/json, text/javascript, */*; q=0.01"; request.Referer = Referer; request.Headers["Accept-Language"] = "en-US,en;q=0.5"; request.UserAgent = DefaultUserAgent; request.ContentType = "application/x-www-form-urlencoded"; request.Headers["Pragma"] = "no-cache"; if (headers != null) { foreach (var header in headers) { request.Headers.Add(header.Key, header.Value); } } if (cookies != null) { request.CookieContainer = new CookieContainer(); request.CookieContainer.Add(cookies); } else { request.CookieContainer = new CookieContainer(); request.CookieContainer.Add(Cookies); } if (!string.IsNullOrEmpty(userAgent)) { request.UserAgent = userAgent; } else { request.UserAgent = DefaultUserAgent; } if (timeout.HasValue) { request.Timeout = timeout.Value * 1000; } request.Expect = string.Empty; //如果需要POST数据 if (!string.IsNullOrEmpty(filePath)) { using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { BinaryReader r = new BinaryReader(fs); //时间戳 string strBoundary = "----------" + DateTime.Now.Ticks.ToString("x"); byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + strBoundary + "\r\n"); //请求头部信息 StringBuilder sb = new StringBuilder(); sb.Append("--"); sb.Append(strBoundary); sb.Append("\r\n"); sb.Append("Content-Disposition: form-data; name=\""); sb.Append("file"); sb.Append("\"; filename=\""); sb.Append(fs.Name); sb.Append("\""); sb.Append("\r\n"); sb.Append("Content-Type: "); sb.Append("application/octet-stream"); sb.Append("\r\n"); sb.Append("\r\n"); string strPostHeader = sb.ToString(); byte[] postHeaderBytes = Encoding.UTF8.GetBytes(strPostHeader); request.ContentType = "multipart/form-data; boundary=" + strBoundary; long length = fs.Length + postHeaderBytes.Length + boundaryBytes.Length; request.ContentLength = length; //开始上传时间 DateTime startTime = DateTime.Now; byte[] filecontent = new byte[fs.Length]; fs.Read(filecontent, 0, filecontent.Length); using (Stream stream = request.GetRequestStream()) { //发送请求头部消息 stream.Write(postHeaderBytes, 0, postHeaderBytes.Length); stream.Write(filecontent, 0, filecontent.Length); //添加尾部的时间戳 stream.Write(boundaryBytes, 0, boundaryBytes.Length); } } } var v = request.GetResponse() as HttpWebResponse; Cookies.Add(request.CookieContainer.GetCookies(new Uri("http://" + new Uri(url).Host))); Cookies.Add(request.CookieContainer.GetCookies(new Uri("https://" + new Uri(url).Host))); Cookies.Add(v.Cookies); if (Debug) { Console.WriteLine("OK"); } return(v); }
/// <summary> /// 创建POST方式的HTTP请求 /// </summary> /// <param name="url">请求的URL</param> /// <param name="parameters">随同请求POST的参数名称及参数值字典</param> /// <param name="timeout">请求的超时时间</param> /// <param name="userAgent">请求的客户端浏览器信息,可以为空</param> /// <param name="requestEncoding">发送HTTP请求时所用的编码</param> /// <param name="cookies">随同HTTP请求发送的Cookie信息,如果不需要身份验证可以为空</param> /// <returns></returns> public HttpWebResponse CreatePostHttpResponse(string url, string parameters, Encoding requestEncoding, int?timeout = 300, string userAgent = "", CookieCollection cookies = null, string Referer = "", Dictionary <string, string> headers = null) { if (Debug) { Console.Write("Start Post Url:{0} ,parameters:{1} ", url, parameters); } if (string.IsNullOrEmpty(url)) { throw new ArgumentNullException("url"); } if (requestEncoding == null) { throw new ArgumentNullException("requestEncoding"); } HttpWebRequest request = null; //如果是发送HTTPS请求 if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase)) { ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); request = WebRequest.Create(url) as HttpWebRequest; request.ProtocolVersion = HttpVersion.Version10; } else { request = WebRequest.Create(url) as HttpWebRequest; } request.Method = "POST"; request.Headers.Add("Accept-Language", "zh-CN,en-GB;q=0.5"); request.Method = "POST"; request.Accept = "text/html, application/xhtml+xml, */*"; request.Referer = Referer; request.Headers["Accept-Language"] = "en-US,en;q=0.5"; request.UserAgent = DefaultUserAgent; request.ContentType = "application/x-www-form-urlencoded"; request.Headers["Pragma"] = "no-cache"; if (cookies != null) { request.CookieContainer = new CookieContainer(); request.CookieContainer.Add(cookies); } else { request.CookieContainer = new CookieContainer(); request.CookieContainer.Add(Cookies); } if (headers != null) { foreach (var header in headers) { request.Headers.Add(header.Key, header.Value); } } if (!string.IsNullOrEmpty(userAgent)) { request.UserAgent = userAgent; } else { request.UserAgent = DefaultUserAgent; } if (timeout.HasValue) { request.Timeout = timeout.Value * 1000; } request.Expect = string.Empty; //如果需要POST数据 if (!string.IsNullOrEmpty(parameters)) { byte[] data = requestEncoding.GetBytes(parameters); using (Stream stream = request.GetRequestStream()) { stream.Write(data, 0, data.Length); } } var v = request.GetResponse() as HttpWebResponse; Cookies.Add(request.CookieContainer.GetCookies(new Uri("http://" + new Uri(url).Host))); Cookies.Add(request.CookieContainer.GetCookies(new Uri("https://" + new Uri(url).Host))); Cookies.Add(v.Cookies); if (Debug) { Console.WriteLine("OK"); } return(v); }
public static void IsSynchronized_Get_Success() { ICollection cc = new CookieCollection(); Assert.False(cc.IsSynchronized); }
public static async Task <StringBuilder> PostWebPage(string Url, Dictionary <string, string> postData, string RefererUrl, CookieCollection cookieCollection, Action <string> userNotify = null) { StringBuilder BodyString = new StringBuilder(); bool urlCorrect = Uri.TryCreate(Url, UriKind.Absolute, out Uri URI) && (URI.Scheme == Uri.UriSchemeHttp || URI.Scheme == Uri.UriSchemeHttps); if (!urlCorrect) { Debug.Write("GetWeb." + System.Reflection.MethodBase.GetCurrentMethod().Name + " => URL not correct"); return(BodyString); } var BaseURI = new Uri(URI.GetLeftPart(UriPartial.Authority)); var PathURI = new Uri(URI.PathAndQuery); using (var handler = new HttpClientHandler()) { if (cookieCollection != null) { var cookies = new CookieContainer(); cookies.Add(cookieCollection); handler.UseCookies = true; handler.CookieContainer = cookies; } else { handler.UseCookies = false; } using (HttpClient client = new HttpClient(handler) { Timeout = TimeSpan.FromSeconds(6) }) { try { client.BaseAddress = BaseURI; client.DefaultRequestHeaders.Add("X-Requested-With", "XMLHttpRequest"); var keyValues = new List <KeyValuePair <string, string> >(); foreach (var item in postData) { keyValues.Add(new KeyValuePair <string, string>(item.Key, item.Value)); } var inputContent = new FormUrlEncodedContent(postData); using (HttpResponseMessage response = await client.PostAsync(PathURI, inputContent).ConfigureAwait(false)) { if (!response.IsSuccessStatusCode) { Debug.Write("GetAdvertListAsync(AdvertSearch searchParams) => !response.IsSuccessStatusCode"); userNotify?.Invoke("Niepoprawna odpowiedź z serwera"); return(BodyString); } using (HttpContent content = response.Content) { var byteArray = await content.ReadAsStringAsync(); BodyString.Append(byteArray); } } } catch (System.Threading.Tasks.TaskCanceledException) { Debug.Write("PostWebPage => System.Threading.Tasks.TaskCanceledException"); userNotify?.Invoke("Błąd połączenia. Przekroczono limit połączenia"); return(BodyString); } catch (Exception e) { Debug.Write("PostWebPage => " + e.Message); userNotify?.Invoke("Błąd połączenia z serwerem."); return(BodyString); } } return(BodyString); } }
public static void Add_Cookie_Invalid() { CookieCollection cc = new CookieCollection(); Assert.Throws<ArgumentNullException>(() => cc.Add((Cookie)null)); }
private static CookieCollection ConvertCookieArraysToCookieCollection(ArrayList al, string strHost) { CookieCollection cc = new CookieCollection(); int alcount = al.Count; string strEachCook; string[] strEachCookParts; for (int i = 0; i < alcount; i++) { strEachCook = al[i].ToString(); strEachCookParts = strEachCook.Split(';'); int intEachCookPartsCount = strEachCookParts.Length; string[] NameValuePairTemp; Cookie cookTemp = new Cookie(); for (int j = 0; j < intEachCookPartsCount; j++) { if (j == 0) { string strCNameAndCValue = strEachCookParts[j]; if (strCNameAndCValue.Length > 0) { int firstEqual = strCNameAndCValue.IndexOf("="); string firstName = strCNameAndCValue.Substring(0, firstEqual); string allValue = strCNameAndCValue.Substring(firstEqual + 1, strCNameAndCValue.Length - (firstEqual + 1)); cookTemp.Name = firstName; cookTemp.Value = allValue; } continue; } string strPNameAndPValue; if (strEachCookParts[j].IndexOf("path", StringComparison.OrdinalIgnoreCase) >= 0) { strPNameAndPValue = strEachCookParts[j]; if (strPNameAndPValue.Length > 0) { NameValuePairTemp = strPNameAndPValue.Split('='); if (NameValuePairTemp[1].Length > 0) { cookTemp.Path = NameValuePairTemp[1]; } else { cookTemp.Path = "/"; } } continue; } if (strEachCookParts[j].IndexOf("domain", StringComparison.OrdinalIgnoreCase) >= 0) { strPNameAndPValue = strEachCookParts[j]; if (strPNameAndPValue.Length > 0) { NameValuePairTemp = strPNameAndPValue.Split('='); if (NameValuePairTemp[1].Length > 0) { cookTemp.Domain = NameValuePairTemp[1]; } else { cookTemp.Domain = strHost; } } continue; } } if (cookTemp.Path.Length == 0) { cookTemp.Path = "/"; } if (cookTemp.Domain.Length == 0) { cookTemp.Domain = strHost; } cc.Add(cookTemp); } return(cc); }
internal void AddHeader(string header) { int colon = header.IndexOf(':'); if (colon == -1 || colon == 0) { context.ErrorMessage = "Bad Request"; context.ErrorStatus = 400; return; } string name = header.Substring(0, colon).Trim(); string val = header.Substring(colon + 1).Trim(); string lower = name.ToLower(CultureInfo.InvariantCulture); headers.SetInternal(name, val); switch (lower) { case "accept-language": user_languages = val.Split(','); // yes, only split with a ',' break; case "accept": accept_types = val.Split(','); // yes, only split with a ',' break; case "content-length": try { //TODO: max. content_length? content_length = Int64.Parse(val.Trim()); if (content_length < 0) context.ErrorMessage = "Invalid Content-Length."; cl_set = true; } catch { context.ErrorMessage = "Invalid Content-Length."; } break; case "content-type": { var contents = val.Split(';'); foreach (var content in contents) { var tmp = content.Trim(); if (tmp.StartsWith("charset")) { var charset = tmp.GetValue("="); if (charset != null && charset.Length > 0) { try { // Support upnp/dlna devices - CONTENT-TYPE: text/xml ; charset="utf-8"\r\n charset = charset.Trim('"'); var index = charset.IndexOf('"'); if (index != -1) charset = charset.Substring(0, index); content_encoding = Encoding.GetEncoding(charset); } catch { context.ErrorMessage = "Invalid Content-Type header: " + charset; } } break; } } } break; case "referer": try { referrer = new Uri(val); } catch { referrer = new Uri("http://someone.is.screwing.with.the.headers.com/"); } break; case "cookie": if (cookies == null) cookies = new CookieCollection(); string[] cookieStrings = val.Split(new char[] { ',', ';' }); Cookie current = null; int version = 0; foreach (string cookieString in cookieStrings) { string str = cookieString.Trim(); if (str.Length == 0) continue; if (str.StartsWith("$Version")) { version = Int32.Parse(Unquote(str.Substring(str.IndexOf('=') + 1))); } else if (str.StartsWith("$Path")) { if (current != null) current.Path = str.Substring(str.IndexOf('=') + 1).Trim(); } else if (str.StartsWith("$Domain")) { if (current != null) current.Domain = str.Substring(str.IndexOf('=') + 1).Trim(); } else if (str.StartsWith("$Port")) { if (current != null) current.Port = str.Substring(str.IndexOf('=') + 1).Trim(); } else { if (current != null) { cookies.Add(current); } current = new Cookie(); int idx = str.IndexOf('='); if (idx > 0) { current.Name = str.Substring(0, idx).Trim(); current.Value = str.Substring(idx + 1).Trim(); } else { current.Name = str.Trim(); current.Value = String.Empty; } current.Version = version; } } if (current != null) { cookies.Add(current); } break; } }
/// <summary> /// 获取MINE文件 /// </summary> /// <param name="url"></param> /// <returns></returns> public Byte[] HttpGetMine(String url) { Byte[] mine = null; try { ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckCertificate); HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url); request.UserAgent = _useragent; request.Timeout = _timeout; request.ContentType = _contenttype; request.Accept = _accept; request.Method = "GET"; request.Referer = url; request.KeepAlive = true; request.AllowAutoRedirect = true; request.UnsafeAuthenticatedConnectionSharing = true; request.CookieContainer = new CookieContainer(); //据说能提高性能 request.Proxy = null; if (_cookiecollection != null) { foreach (Cookie c in _cookiecollection) { c.Domain = request.Host; } request.CookieContainer.Add(_cookiecollection); } foreach (KeyValuePair <String, String> hd in _headers) { request.Headers[hd.Key] = hd.Value; } HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream stream = response.GetResponseStream(); MemoryStream ms = new MemoryStream(); byte[] b = new byte[1024]; while (true) { int s = stream.Read(b, 0, b.Length); ms.Write(b, 0, s); if (s == 0 || s < b.Length) { break; } } mine = ms.ToArray(); ms.Close(); if (request.CookieContainer != null) { response.Cookies = request.CookieContainer.GetCookies(request.RequestUri); } if (response.Cookies != null) { _cookiecollection = response.Cookies; } if (response.Headers["Set-Cookie"] != null) { _cookies = response.Headers["Set-Cookie"]; } stream.Close(); stream.Dispose(); response.Close(); return(mine); } catch (Exception e) { Trace.WriteLine("HttpGetMine Error: " + e.Message); return(null); } }
public void SetCookie(Cookie cookie) { if (cookie == null) throw new ArgumentNullException ("cookie"); if (cookies != null) { if (FindCookie (cookie)) throw new ArgumentException ("The cookie already exists."); } else { cookies = new CookieCollection (); } cookies.Add (cookie); }
/// <summary> /// 发送POST请求 /// </summary> /// <param name="url"></param> /// <param name="data"></param> /// <param name="refer"></param> /// <returns></returns> public String HttpPost(String url, String data, String refer) { String html; try { ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckCertificate); HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url); request.UserAgent = _useragent; request.Timeout = _timeout; request.Referer = refer; request.ContentType = _contenttype; request.Accept = _accept; request.Method = "POST"; request.KeepAlive = true; request.AllowAutoRedirect = true; request.CookieContainer = new CookieContainer(); //据说能提高性能 request.Proxy = null; if (_cookiecollection != null) { foreach (Cookie c in _cookiecollection) { c.Domain = request.Host; if (c.Domain.IndexOf(':') > 0) { c.Domain = c.Domain.Remove(c.Domain.IndexOf(':')); } } request.CookieContainer.Add(_cookiecollection); } foreach (KeyValuePair <String, String> hd in _headers) { request.Headers[hd.Key] = hd.Value; } byte[] buffer = _encoding.GetBytes(data.Trim()); request.ContentLength = buffer.Length; request.GetRequestStream().Write(buffer, 0, buffer.Length); request.GetRequestStream().Close(); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); html = GetStringFromResponse(response); if (request.CookieContainer != null) { response.Cookies = request.CookieContainer.GetCookies(request.RequestUri); } if (response.Cookies != null) { _cookiecollection = response.Cookies; } if (response.Headers["Set-Cookie"] != null) { string tmpcookie = response.Headers["Set-Cookie"]; _cookiecollection.Add(ConvertCookieString(tmpcookie)); } response.Close(); return(html); } catch (Exception e) { Trace.WriteLine("HttpPost Error: " + e.Message); return(String.Empty); } }
public static string PostFile(string url, string refernorigin, byte[] filecontent, string filefieldname, string filetype, string filename, Dictionary <string, string> PostInfo = null, CookieCollection cookies = null) { //上传文件以及相关参数 //POST请求分隔符,可任意定制 string Boundary = "----WebKitFormBoundarySKNbhflQaXvVSUIb"; //构造POST请求体 StringBuilder PostContent = new StringBuilder("\r\n--" + Boundary); byte[] ContentEnd = Encoding.UTF8.GetBytes("--\r\n");//请求体末尾,后面会用到 //组成普通参数信息 foreach (KeyValuePair <string, string> item in PostInfo) { PostContent.Append("\r\n") .Append("Content-Disposition: form-data; name=\"") .Append(item.Key + "\"").Append("\r\n") .Append("\r\n").Append(item.Value).Append("\r\n") .Append("--").Append(Boundary); } //转换为二进制数组,后面会用到 byte[] PostContentByte = Encoding.UTF8.GetBytes(PostContent.ToString()); //文件信息 byte[] UpdateFile = filecontent;//二进制 StringBuilder FileContent = new StringBuilder(); FileContent.Append("--").Append(Boundary).Append("\r\n") .Append("Content-Disposition:form-data; name=\"" + filefieldname + "\"; ") .Append("filename=\"") .Append(filename + "\"") .Append("\r\n") .Append("Content-Type:") .Append(filetype) .Append("\r\n\r\n"); byte[] FileContentByte = Encoding.UTF8.GetBytes(FileContent.ToString()); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); //request.Proxy = new WebProxy("http://127.0.0.1:8888", false) ; request.Method = "POST"; request.Timeout = 20000; request.CookieContainer = ToContainer(cookies); request.Referer = refernorigin; request.Headers.Add("origin", refernorigin); //这里确定了分隔符是什么 request.ContentType = "multipart/form-data;boundary=" + Boundary; request.ContentLength = PostContentByte.Length + FileContentByte.Length + UpdateFile.Length + ContentEnd.Length; //定义请求流 Stream myRequestStream = request.GetRequestStream(); myRequestStream.Write(FileContentByte, 0, FileContentByte.Length); //写入文件信息 myRequestStream.Write(UpdateFile, 0, UpdateFile.Length); //文件写入请求流中 myRequestStream.Write(PostContentByte, 0, PostContentByte.Length); //写入参数 myRequestStream.Write(ContentEnd, 0, ContentEnd.Length); //写入结尾 HttpWebResponse response = (HttpWebResponse)request.GetResponse(); //获取返回值 Stream myResponseStream = response.GetResponseStream(); StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8")); string retString = myStreamReader.ReadToEnd(); myRequestStream.Close(); myStreamReader.Close(); myResponseStream.Close(); return(retString); }
public void InitCookie() { _cookies = ""; _cookiecollection = null; _headers.Clear(); }
public void ClearCookies() { _cookies = new CookieCollection(); }
/// <summary> /// 创建GET方式的HTTP请求 /// </summary> /// <param name="url">请求的URL</param> /// <param name="timeout">请求的超时时间</param> /// <param name="userAgent">请求的客户端浏览器信息,可以为空</param> /// <param name="cookies">随同HTTP请求发送的Cookie信息,如果不需要身份验证可以为空</param> /// <returns></returns> public static HttpWebResponse CreateGetHttpResponse(string url, int?timeout, string userAgent, string referer, CookieCollection cookies, IDictionary <string, string> headers) { if (string.IsNullOrEmpty(url)) { throw new ArgumentNullException("url"); } //HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; HttpWebRequest request = null; //如果是发送HTTPS请求 if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase)) { ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; request = WebRequest.Create(url) as HttpWebRequest; request.ProtocolVersion = HttpVersion.Version10; } else { request = WebRequest.Create(url) as HttpWebRequest; } request.Method = "GET"; request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"; request.UserAgent = DefaultUserAgent; request.Referer = referer; request.UseDefaultCredentials = true; if (!(headers == null || headers.Count == 0)) { foreach (string key in headers.Keys) { request.Headers.Add(key, headers[key]); } } if (!string.IsNullOrEmpty(userAgent)) { request.UserAgent = userAgent; } if (timeout.HasValue) { request.Timeout = timeout.Value; } if (cookies != null) { request.CookieContainer = new CookieContainer(); request.CookieContainer.Add(cookies); } request.AllowAutoRedirect = true; return(request.GetResponse() as HttpWebResponse); }
//update cookiesToUpdate to localCookies public void updateLocalCookies(CookieCollection cookiesToUpdate, ref CookieCollection localCookies) { updateLocalCookies(cookiesToUpdate, ref localCookies, null); }
/// <summary> /// 创建POST方式的HTTP请求 /// </summary> /// <param name="url">请求的URL</param> /// <param name="parameters">随同请求POST的参数名称及参数值字典</param> /// <param name="timeout">请求的超时时间</param> /// <param name="userAgent">请求的客户端浏览器信息,可以为空</param> /// <param name="requestEncoding">发送HTTP请求时所用的编码</param> /// <param name="cookies">随同HTTP请求发送的Cookie信息,如果不需要身份验证可以为空</param> /// <returns></returns> public static HttpWebResponse CreatePostHttpResponse(string url, IDictionary <string, string> parameters, int?timeout, string userAgent, string referer, Encoding requestEncoding, CookieCollection cookies, IDictionary <string, string> headers) { if (string.IsNullOrEmpty(url)) { throw new ArgumentNullException("url"); } if (requestEncoding == null) { requestEncoding = Encoding.UTF8; //throw new ArgumentNullException("requestEncoding"); } HttpWebRequest request = null; //如果是发送HTTPS请求 if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase)) { ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; request = WebRequest.Create(url) as HttpWebRequest; request.ProtocolVersion = HttpVersion.Version10; } else { request = WebRequest.Create(url) as HttpWebRequest; } request.Method = "POST"; request.Accept = "*/*"; //request.Headers.Add("__RequestVerificationToken", "JgFeMIBz6J22tKUXuPwQ4-HIfVt9YblgPQPfM6Lw6dzXrY98dUemdMPpbL87WX0AgbMRQ55A7C5twtZ527EU6pFn8HY1"); request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8"; if (!(headers == null || headers.Count == 0)) { foreach (string key in headers.Keys) { request.Headers.Add(key, headers[key]); } } //request.ContentType = "application/json"; request.Referer = referer; if (!string.IsNullOrEmpty(userAgent)) { request.UserAgent = userAgent; } else { request.UserAgent = DefaultUserAgent; } if (timeout.HasValue) { request.Timeout = timeout.Value; } if (cookies != null) { request.CookieContainer = new CookieContainer(); request.CookieContainer.Add(cookies); } //如果需要POST数据 if (!(parameters == null || parameters.Count == 0)) { StringBuilder buffer = new StringBuilder(); int i = 0; foreach (string key in parameters.Keys) { if (i > 0) { buffer.AppendFormat("&{0}={1}", key, parameters[key]); } else { buffer.AppendFormat("{0}={1}", key, parameters[key]); } i++; } byte[] data = requestEncoding.GetBytes(buffer.ToString()); using (Stream stream = request.GetRequestStream()) { stream.Write(data, 0, data.Length); } } return(request.GetResponse() as HttpWebResponse); }
// given a cookie name ckName, get its value from CookieCollection cookies public bool getCookieVal(string ckName, ref CookieCollection cookies, out string ckVal) { //string ckVal = ""; ckVal = ""; bool gotValue = false; foreach (Cookie ck in cookies) { if (ck.Name == ckName) { gotValue = true; ckVal = ck.Value; break; } } return gotValue; }
//add a single cookie to cookies, if already exist, update its value public void addCookieToCookies(Cookie toAdd, ref CookieCollection cookies, bool overwriteDomain) { bool found = false; if (cookies.Count > 0) { foreach (Cookie originalCookie in cookies) { if (originalCookie.Name == toAdd.Name) { // !!! for different domain, cookie is not same, // so should not set the cookie value here while their domains is not same // only if it explictly need overwrite domain if ((originalCookie.Domain == toAdd.Domain) || ((originalCookie.Domain != toAdd.Domain) && overwriteDomain)) { //here can not force convert CookieCollection to HttpCookieCollection, //then use .remove to remove this cookie then add // so no good way to copy all field value originalCookie.Value = toAdd.Value; originalCookie.Domain = toAdd.Domain; originalCookie.Expires = toAdd.Expires; originalCookie.Version = toAdd.Version; originalCookie.Path = toAdd.Path; //following fields seems should not change //originalCookie.HttpOnly = toAdd.HttpOnly; //originalCookie.Secure = toAdd.Secure; found = true; break; } } } } if (!found) { if (toAdd.Domain != "") { // if add the null domain, will lead to follow req.CookieContainer.Add(cookies) failed !!! cookies.Add(toAdd); } } }
public static void SyncRoot_Get_Success() { ICollection cc = new CookieCollection(); Assert.Equal(cc, cc.SyncRoot); }
public static void Add_Cookie_Success() { CookieCollection cc = CreateCookieCollection1(); Assert.Equal(5, cc.Count); }
internal void AddHeader(string header) { var colon = header.IndexOf(':'); if (colon == -1 || colon == 0) { _context.ErrorMessage = "Bad Request"; _context.ErrorStatus = 400; return; } var name = header.Substring(0, colon).Trim(); var val = header.Substring(colon + 1).Trim(); var lower = name.ToLowerInvariant(); Headers.Set(name, val); switch (lower) { case "accept-language": UserLanguages = val.Split(','); // yes, only split with a ',' break; case "accept": AcceptTypes = val.Split(','); // yes, only split with a ',' break; case "content-length": try { //TODO: max. content_length? ContentLength64 = long.Parse(val.Trim()); if (ContentLength64 < 0) _context.ErrorMessage = "Invalid Content-Length."; _clSet = true; } catch { _context.ErrorMessage = "Invalid Content-Length."; } break; case "referer": try { UrlReferrer = new Uri(val); } catch { UrlReferrer = new Uri("http://someone.is.screwing.with.the.headers.com/"); } break; case "cookie": if (_cookies == null) _cookies = new CookieCollection(); var cookieStrings = val.Split(',', ';'); Cookie current = null; var version = 0; foreach (var cookieString in cookieStrings) { var str = cookieString.Trim(); if (str.Length == 0) continue; if (str.StartsWith("$Version")) { version = int.Parse(str.Substring(str.IndexOf('=') + 1).Unquote()); } else if (str.StartsWith("$Path")) { if (current != null) current.Path = str.Substring(str.IndexOf('=') + 1).Trim(); } else if (str.StartsWith("$Domain")) { if (current != null) current.Domain = str.Substring(str.IndexOf('=') + 1).Trim(); } else if (str.StartsWith("$Port")) { if (current != null) current.Port = str.Substring(str.IndexOf('=') + 1).Trim(); } else { if (current != null) { _cookies.Add(current); } current = new Cookie(); var idx = str.IndexOf('='); if (idx > 0) { current.Name = str.Substring(0, idx).Trim(); current.Value = str.Substring(idx + 1).Trim(); } else { current.Name = str.Trim(); current.Value = string.Empty; } current.Version = version; } } if (current != null) { _cookies.Add(current); } break; } }
public static void Add_Cookie_Invalid() { CookieCollection cc = new CookieCollection(); Assert.Throws <ArgumentNullException>(() => cc.Add((Cookie)null)); }
public CookieManager() { _Cookies = new CookieCollection(); }
public ThirdPartAPIs(CookieCollection c) { CookieContext = c; }
//---------------------------------------以下代码请勿更动--------------------------------------------------------------------------------------------- /// <summary> /// 创建POST方式的HTTP请求 /// </summary> /// <param name="url">请求的URL</param> /// <param name="parameters">随同请求POST的参数名称及参数值字典</param> /// <param name="timeout">请求的超时时间</param> /// <param name="userAgent">请求的客户端浏览器信息,可以为空</param> /// <param name="requestEncoding">发送HTTP请求时所用的编码</param> /// <param name="cookies">随同HTTP请求发送的Cookie信息,如果不需要身份验证可以为空</param> /// <returns></returns> public static HttpWebResponse CreatePostHttpResponse(string url,IDictionary<string,string> parameters,int timeout, string userAgent,Encoding requestEncoding,CookieCollection cookies) { if (string.IsNullOrEmpty(url)) { throw new ArgumentNullException("url"); } if(requestEncoding==null) { throw new ArgumentNullException("requestEncoding"); } HttpWebRequest request=null; request = WebRequest.Create(url) as HttpWebRequest; request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; if (!string.IsNullOrEmpty(userAgent)) { request.UserAgent = userAgent; } else { request.UserAgent = DefaultUserAgent; } if (timeout!=null) { request.Timeout = timeout; } if (cookies != null) { request.CookieContainer = new CookieContainer(); request.CookieContainer.Add(cookies); } //如果需要POST数据 if(!(parameters==null||parameters.Count==0)) { StringBuilder buffer = new StringBuilder(); int i = 0; foreach (string key in parameters.Keys) { if (i > 0) { buffer.AppendFormat("&{0}={1}", key, parameters[key]); } else { buffer.AppendFormat("{0}={1}", key, parameters[key]); } i++; } byte[] data = requestEncoding.GetBytes(buffer.ToString()); using (Stream stream = request.GetRequestStream()) { stream.Write(data, 0, data.Length); } } return request.GetResponse() as HttpWebResponse; }
/// <summary> /// 发送GET请求 /// </summary> /// <param name="url">请求的URL地址</param> /// <param name="encodingName">编码格式</param> /// <param name="timeout">超时时间 以毫秒为单位</param> /// <param name="cookies">请求时,附带的Cookie值</param> /// <param name="headers">请求时,附带的Header值</param> public static string SendGetRequestCore(string url, string encodingName, int?timeout, CookieCollection cookies, NameValueCollection headers) { HttpWebRequest request = null; try { // 创建HTTP请求 request = CreateHttpRequest(url, "GET", timeout); // 写入Cookie WriteRequestCookie(request, cookies); // 写入header值 WriteRequestHeader(request, headers); // 接收HTTP响应 return(ReceiveResponse(request, encodingName)); } finally { if (request != null) { request.Abort(); } } }
async Task SetCookieAsync(string header) { if (_cookieCollection == null) _cookieCollection = new CookieCollection(); var parser = new CookieParser(header); foreach (var cookie in parser.Parse()) { if (cookie.Domain == "") { cookie.Domain = _uri.Host; cookie.HasDomain = false; } if (cookie.HasDomain && !await CookieContainer.CheckSameOriginAsync(_uri, cookie.Domain).ConfigureAwait(false)) continue; _cookieCollection.Add(cookie); if (_cookieContainer != null) await _cookieContainer.AddAsync(_uri, cookie).ConfigureAwait(false); } }
public static string SendPostRequestjson(string url, string content, string encodingName, int?timeout, CookieCollection cookies, NameValueCollection headers, string contentType) { HttpWebRequest request = null; try { // 创建HTTP请求 request = CreateHttpRequest(url, "POST", timeout); // 写入Cookie WriteRequestCookie(request, cookies); // 写入header值 WriteRequestHeader(request, headers); if (!string.IsNullOrWhiteSpace(contentType)) { request.ContentType = contentType; } // 写入POST数据 WriteRequestjsonContent(request, content, encodingName); // 接收HTTP响应 return(ReceiveResponse(request, encodingName)); } finally { if (request != null) { request.Abort(); } } }
/// <summary> /// 创建POST方式的HTTP请求 /// </summary> public static HttpWebResponse CreatePostHttpResponse(string url, IDictionary <string, string> parameters, int timeout, string userAgent, CookieCollection cookies) { HttpWebRequest request = null; //如果是发送HTTPS请求 if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase)) { //ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); request = WebRequest.Create(url) as HttpWebRequest; //request.ProtocolVersion = HttpVersion.Version10; } else { request = WebRequest.Create(url) as HttpWebRequest; } request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; //设置代理UserAgent和超时 //request.UserAgent = userAgent; request.Timeout = timeout; if (cookies != null) { request.CookieContainer = new CookieContainer(); request.CookieContainer.Add(cookies); } //发送POST数据 if (!(parameters == null || parameters.Count == 0)) { StringBuilder buffer = new StringBuilder(); int i = 0; foreach (string key in parameters.Keys) { if (i > 0) { buffer.AppendFormat("&{0}={1}", key, parameters[key]); } else { buffer.AppendFormat("{0}={1}", key, parameters[key]); i++; } } byte[] data = Encoding.ASCII.GetBytes(buffer.ToString()); using (Stream stream = request.GetRequestStream()) { stream.Write(data, 0, data.Length); } } string[] values = request.Headers.GetValues("Content-Type"); return(request.GetResponse() as HttpWebResponse); }
// parse the Set-Cookie string (in http response header) to cookies // Note: auto omit to parse the abnormal cookie string // normal example for 'setCookieStr': // MSPOK= ; expires=Thu, 30-Oct-1980 16:00:00 GMT;domain=login.live.com;path=/;HTTPOnly= ;version=1,PPAuth=Cuyf3Vp2wolkjba!TOr*0v22UMYz36ReuiwxZZBc8umHJYPlRe4qupywVFFcIpbJyvYZ5ZDLBwV4zRM1UCjXC4tUwNuKvh21iz6gQb0Tu5K7Z62!TYGfowB9VQpGA8esZ7iCRucC7d5LiP3ZAv*j4Z3MOecaJwmPHx7!wDFdAMuQUZURhHuZWJiLzHP1j8ppchB2LExnlHO6IGAdZo1f0qzSWsZ2hq*yYP6sdy*FdTTKo336Q1B0i5q8jUg1Yv6c2FoBiNxhZSzxpuU0WrNHqSytutP2k4!wNc6eSnFDeouX; domain=login.live.com;secure= ;path=/;HTTPOnly= ;version=1,PPLState=1; domain=.live.com;path=/;version=1,MSPShared=1; expires=Wed, 30-Dec-2037 16:00:00 GMT;domain=login.live.com;path=/;HTTPOnly= ;version=1,MSPPre= ;domain=login.live.com;path=/;Expires=Thu, 30-Oct-1980 16:00:00 GMT,MSPCID= ; HTTPOnly= ; domain=login.live.com;path=/;Expires=Thu, 30-Oct-1980 16:00:00 GMT,RPSTAuth=EwDoARAnAAAUWkziSC7RbDJKS1VkhugDegv7L0eAAOfCAY2+pKwbV5zUlu3XmBbgrQ8EdakmdSqK9OIKfMzAbnU8fuwwEi+FKtdGSuz/FpCYutqiHWdftd0YF21US7+1bPxuLJ0MO+wVXB8GtjLKZaA0xCXlU5u01r+DOsxSVM777DmplaUc0Q4O1+Pi9gX9cyzQLAgRKmC/QtlbVNKDA2YAAAhIwqiXOVR/DDgBocoO/n0u48RFGh79X2Q+gO4Fl5GMc9Vtpa7SUJjZCCfoaitOmcxhEjlVmR/2ppdfJx3Ykek9OFzFd+ijtn7K629yrVFt3O9q5L0lWoxfDh5/daLK7lqJGKxn1KvOew0SHlOqxuuhYRW57ezFyicxkxSI3aLxYFiqHSu9pq+TlITqiflyfcAcw4MWpvHxm9on8Y1dM2R4X3sxuwrLQBpvNsG4oIaldTYIhMEnKhmxrP6ZswxzteNqIRvMEKsxiksBzQDDK/Cnm6QYBZNsPawc6aAedZioeYwaV3Z/i3tNrAUwYTqLXve8oG6ZNXL6WLT/irKq1EMilK6Cw8lT3G13WYdk/U9a6YZPJC8LdqR0vAHYpsu/xRF39/On+xDNPE4keIThJBptweOeWQfsMDwvgrYnMBKAMjpLZwE=; domain=.live.com;path=/;HTTPOnly= ;version=1,RPSTAuthTime=1328679636; domain=login.live.com;path=/;HTTPOnly= ;version=1,MSPAuth=2OlAAMHXtDIFOtpaK1afG2n*AAxdfCnCBlJFn*gCF8gLnCa1YgXEfyVh2m9nZuF*M7npEwb4a7Erpb*!nH5G285k7AswJOrsr*gY29AVAbsiz2UscjIGHkXiKrTvIzkV2M; domain=.live.com;path=/;HTTPOnly= ;version=1,MSPProf=23ci9sti6DZRrkDXfTt1b3lHhMdheWIcTZU2zdJS9!zCloHzMKwX30MfEAcCyOjVt*5WeFSK3l2ZahtEaK7HPFMm3INMs3r!JxI8odP9PYRHivop5ryohtMYzWZzj3gVVurcEr5Bg6eJJws7rXOggo3cR4FuKLtXwz*FVX0VWuB5*aJhRkCT1GZn*L5Pxzsm9X; domain=.live.com;path=/;HTTPOnly= ;version=1,MSNPPAuth=CiGSMoUOx4gej8yQkdFBvN!gvffvAhCPeWydcrAbcg!O2lrhVb4gruWSX5NZCBPsyrtZKmHLhRLTUUIxxPA7LIhqW5TCV*YcInlG2f5hBzwzHt!PORYbg79nCkvw65LKG399gRGtJ4wvXdNlhHNldkBK1jVXD4PoqO1Xzdcpv4sj68U6!oGrNK5KgRSMXXpLJmCeehUcsRW1NmInqQXpyanjykpYOcZy0vq!6PIxkj3gMaAvm!1vO58gXM9HX9dA0GloNmCDnRv4qWDV2XKqEKp!A7jiIMWTmHup1DZ!*YCtDX3nUVQ1zAYSMjHmmbMDxRJECz!1XEwm070w16Y40TzuKAJVugo!pyF!V2OaCsLjZ9tdGxGwEQRyi0oWc*Z7M0FBn8Fz0Dh4DhCzl1NnGun9kOYjK5itrF1Wh17sT!62ipv1vI8omeu0cVRww2Kv!qM*LFgwGlPOnNHj3*VulQOuaoliN4MUUxTA4owDubYZoKAwF*yp7Mg3zq5Ds2!l9Q$$; domain=.live.com;path=/;HTTPOnly= ;version=1,MH=MSFT; domain=.live.com;path=/;version=1,MHW=; expires=Thu, 30-Oct-1980 16:00:00 GMT;domain=.live.com;path=/;version=1,MHList=; expires=Thu, 30-Oct-1980 16:00:00 GMT;domain=.live.com;path=/;version=1,NAP=V=1.9&E=bea&C=zfjCKKBD0TqjZlWGgRTp__NiK08Lme_0XFaiKPaWJ0HDuMi2uCXafQ&W=1;domain=.live.com;path=/,ANON=A=DE389D4D076BF47BCAE4DC05FFFFFFFF&E=c44&W=1;domain=.live.com;path=/,MSPVis=$9;domain=login.live.com;path=/,pres=; expires=Thu, 30-Oct-1980 16:00:00 GMT;domain=.live.com;path=/;version=1,LOpt=0; domain=login.live.com;path=/;version=1,WLSSC=EgBnAQMAAAAEgAAACoAASfCD+8dUptvK4kvFO0gS3mVG28SPT3Jo9Pz2k65r9c9KrN4ISvidiEhxXaPLCSpkfa6fxH3FbdP9UmWAa9KnzKFJu/lQNkZC3rzzMcVUMjbLUpSVVyscJHcfSXmpGGgZK4ZCxPqXaIl9EZ0xWackE4k5zWugX7GR5m/RzakyVIzWAFwA1gD9vwYA7Vazl9QKMk/UCjJPECcAAAoQoAAAFwBjcmlmYW4yMDAzQGhvdG1haWwuY29tAE8AABZjcmlmYW4yMDAzQGhvdG1haWwuY29tAAAACUNOAAYyMTM1OTIAAAZlCAQCAAB3F21AAARDAAR0aWFuAAR3YW5nBMgAAUkAAAAAAAAAAAAAAaOKNpqLi/UAANQKMk/Uf0RPAAAAAAAAAAAAAAAADgA1OC4yNDAuMjM2LjE5AAUAAAAAAAAAAAAAAAABBAABAAABAAABAAAAAAAAAAA=; domain=.live.com;secure= ;path=/;HTTPOnly= ;version=1,MSPSoftVis=@72198325083833620@:@; domain=login.live.com;path=/;version=1 // here now support parse the un-correct Set-Cookie: // MSPRequ=/;Version=1;version<=1328770452&id=250915&co=1; path=/;version=1,MSPVis=$9; Version=1;version=1$250915;domain=login.live.com;path=/,MSPSoftVis=@72198325083833620@:@; domain=login.live.com;path=/;version=1,MSPBack=1328770312; domain=login.live.com;path=/;version=1 public CookieCollection parseSetCookie(string setCookieStr, string curDomain) { CookieCollection parsedCookies = new CookieCollection(); // process for expires and Expires field, for it contains ',' //refer: http://www.yaosansi.com/post/682.html // may contains expires or Expires, so following use xpires string commaReplaced = Regex.Replace(setCookieStr, @"xpires=\w{3},\s\d{2}-\w{3}-\d{4}", new MatchEvaluator(_processExpireField)); string[] cookieStrArr = commaReplaced.Split(','); foreach (string cookieStr in cookieStrArr) { Cookie ck = new Cookie(); // recover it back string recoveredCookieStr = Regex.Replace(cookieStr, @"xpires=\w{3}" + replacedChar + @"\s\d{2}-\w{3}-\d{4}", new MatchEvaluator(_recoverExpireField)); if (parseSingleCookie(recoveredCookieStr, ref ck)) { if (needAddThisCookie(ck, curDomain)) { parsedCookies.Add(ck); } } } return parsedCookies; }
public void Add(CookieCollection cookies) {}
/* set current cookies */ public void setCurCookies(CookieCollection cookies) { curCookies = cookies; }
/// <summary> /// Sets the cookies. /// </summary> /// <param name="cookies">The cookies.</param> public void SetCookies(CookieCollection cookies) { if (cookies == null || cookies.Count == 0) return; var headers = Headers; foreach (var cookie in cookies) // TODO: .Sorted) headers.Add("Set-Cookie", cookie.ToString()); //.ToResponseString ()); }
// update cookiesToUpdate to localCookies // if omitUpdateCookies designated, then omit cookies of omitUpdateCookies in cookiesToUpdate public void updateLocalCookies(CookieCollection cookiesToUpdate, ref CookieCollection localCookies, object omitUpdateCookies) { if (cookiesToUpdate.Count > 0) { if (localCookies == null) { localCookies = cookiesToUpdate; } else { foreach (Cookie newCookie in cookiesToUpdate) { if (isContainCookie(newCookie, omitUpdateCookies)) { // need omit process this } else { addCookieToCookies(newCookie, ref localCookies); } } } } }
// "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)"; /// <summary> /// 创建GET方式的HTTP请求 /// </summary> /// <param name="url">请求的URL</param> /// <param name="timeout">请求的超时时间</param> /// <param name="userAgent">请求的客户端浏览器信息,可以为空</param> /// <param name="cookies">随同HTTP请求发送的Cookie信息,如果不需要身份验证可以为空</param> /// <returns></returns> public static HttpWebResponse CreateGetHttpResponse(string url, int?timeout, string userAgent, CookieCollection cookies) { if (string.IsNullOrEmpty(url)) { throw new ArgumentNullException("url"); } HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; request.Method = "GET"; request.UserAgent = DefaultUserAgent; if (!string.IsNullOrEmpty(userAgent)) { request.UserAgent = userAgent; } if (timeout.HasValue) { request.Timeout = timeout.Value; } if (cookies != null) { request.CookieContainer = new CookieContainer(); request.CookieContainer.Add(cookies); } return(request.GetResponse() as HttpWebResponse); }
/// <summary> /// 发送GET请求 /// </summary> /// <param name="url">请求的URL地址</param> /// <param name="encodingName">编码格式</param> /// <param name="timeout">超时时间 以毫秒为单位</param> /// <param name="cookies">请求时,附带的Cookie值</param> /// <param name="headers">请求时,附带的Header值</param> /// <remarks>如何出错,会返回错误信息</remarks> public static string SendGetRequest(string url, string encodingName, int?timeout, CookieCollection cookies, NameValueCollection headers) { try { return(SendGetRequestCore(url, encodingName, timeout, cookies, headers)); } catch (Exception ex) { return(ex.Message); } }