/// <summary> /// 向服务器发送 HTTP POST 请求 /// </summary> /// <param name="url">完整的网页地址 /// <para>必须包含 "http://" 或 "https://"</para> /// </param> /// <param name="data">请求所需的上传数据</param> /// <param name="contentType">Content-Type HTTP 标头</param> /// <param name="referer">参考页链接 /// <para>告知服务器, 访问时的来源地址</para> /// </param> /// <param name="userAgent">User-Agent HTTP 标头</param> /// <param name="accept">Accept HTTP 标头</param> /// <param name="timeout">超时时间</param> /// <param name="cookies">请求附带的 Cookies /// <para>此参数支持自动更新 Cookies, 若 cookieMerge 参数为True, 将合并新旧Cookie</para> /// </param> /// <param name="headers">请求附带的 Headers /// <para>此参数支持自动更新 Headers</para> /// </param> /// <param name="proxy">代理 HttpWebClient 的 WebProxy 实例</param> /// <param name="encoding">文本编码</param> /// <param name="allowAutoRedirect">跟随重定向响应</param> /// <param name="autoCookieMerge">自动Cookie合并</param> /// <returns></returns> public static byte[] Post(string url, byte[] data, string contentType, string referer, string userAgent, string accept, int timeout, ref CookieCollection cookies, ref WebHeaderCollection headers, WebProxy proxy, Encoding encoding, bool allowAutoRedirect = true, bool autoCookieMerge = true) { HttpWebClient httpWebClient = new HttpWebClient(); httpWebClient.ContentType = contentType; httpWebClient.Referer = referer; httpWebClient.UserAgent = userAgent; httpWebClient.Accept = accept; httpWebClient.TimeOut = timeout; httpWebClient.CookieCollection = cookies; httpWebClient.Headers = headers; httpWebClient.Proxy = proxy; httpWebClient.AllowAutoRedirect = allowAutoRedirect; byte[] result = httpWebClient.UploadData(new Uri(url), data); headers = httpWebClient.ResponseHeaders; if (autoCookieMerge) { cookies = UpdateCookie(cookies, httpWebClient.CookieCollection); } else { cookies = httpWebClient.CookieCollection; } return(result); }
/// <summary> /// 向服务器发送 HTTP GET 请求 /// </summary> /// <param name="url">完整的网页地址 /// <para>必须包含 "http://" 或 "https://"</para> /// </param> /// <param name="referer">参考页链接 /// <para>告知服务器, 访问时的来源地址</para> /// </param> /// <param name="userAgent">User-Agent HTTP 标头</param> /// <param name="accept">Accept HTTP 标头</param> /// <param name="timeout">超时时间</param> /// <param name="cookies">请求附带的 Cookies /// <para>此参数支持自动更新 <see cref="CookieContainer"/>, 若 <see cref="AutoCookieMerge"/> 参数为 True, 将合并新旧 Cookie</para> /// </param> /// <param name="headers">请求附带的 Headers /// <para>此参数支持自动更新 <see cref="WebHeaderCollection"/></para> /// </param> /// <param name="proxy">代理 <see cref="HttpWebClient"/> 的 <see cref="WebProxy"/> 实例</param> /// <param name="encoding">文本编码</param> /// <param name="allowAutoRedirect">跟随重定向响应</param> /// <param name="autoCookieMerge">指定自动 <see cref="CookieContainer"/> 合并</param> /// <returns>返回从 Internal 读取的 <see cref="byte"/> 数组</returns> public static byte[] Get(string url, string referer, string userAgent, string accept, int timeout, ref CookieCollection cookies, ref WebHeaderCollection headers, WebProxy proxy, Encoding encoding, bool allowAutoRedirect = true, bool autoCookieMerge = true) { HttpWebClient httpWebClient = new HttpWebClient(); httpWebClient.CookieCollection = cookies; httpWebClient.Headers = headers; httpWebClient.Referer = referer; httpWebClient.UserAgent = userAgent; httpWebClient.Accept = accept; httpWebClient.TimeOut = timeout; httpWebClient.Encoding = encoding; httpWebClient.Proxy = proxy; httpWebClient.AllowAutoRedirect = allowAutoRedirect; httpWebClient.AutoCookieMerge = autoCookieMerge; byte[] result = httpWebClient.DownloadData(new Uri(url)); headers = httpWebClient.ResponseHeaders; cookies = httpWebClient.CookieCollection; return(result); }