一个数据采集项目中,使用 HttpClient 携带COOKIE 对 被采集的系统调用API的方式进行数据请求,使用 HttpClient 过程中发现,通过HttpClient 中 Send方式进行数据请求时 ”JSESSIONID“缺失!
HttpClient 初始化:
1 #region 初始化 HttpClient 2 3 var handler = new HttpClientHandler(); 4 handler.UseCookies = true; 5 handler.AllowAutoRedirect = true; 6 handler.CookieContainer = cookieContainer; 7 handler.Proxy = webProxy; 8 handler.UseProxy = true; 9 HttpClient httpClient = new HttpClient(handler); 10 httpClient.BaseAddress = new Uri("**********"); 11 httpClient.DefaultRequestHeaders.Add("Accept", "application/json, text/javascript, */*; q=0.01"); 12 httpClient.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0;********"); 13 httpClient.DefaultRequestHeaders.Add("Accept-Encoding", "gzip, deflate, br"); 14 15 #endregion 16
分别以 Send 和 Post 两种请求方式请求:
1 #region Send Request 2 3 HttpRequestMessage httpRequestTreeMessage = new HttpRequestMessage(); 4 httpRequestTreeMessage.Method = HttpMethod.Post; 5 httpRequestTreeMessage.RequestUri = new Uri(request.httpClient.BaseAddress + RequestAddress); 6 httpRequestTreeMessage.Content = null; 7 HttpResponseMessage SendResponse = request.httpClient.Send(httpRequestTreeMessage); 8 var SendResponseData = await SendResponse.Content.ReadAsStringAsync(); 9 10 #endregion 11 12 13 #region Post Request 14 15 HttpResponseMessage PostResponse = request.httpClient.PostAsync(RequestAddress,null ).Result; 16 17 var PostResponseData = await PostResponse.Content.ReadAsStringAsync(); 18 19 #endregion
1.Send 方式的请求头:
HttpClient Send
2.Post 方式的请求头:
HttpClient Post
从两次请求头分析,Send 请求 Cookie 不包含 "JSESSIONID",而在 Post 请求 Cookie 中包含 "JSESSIONID" 。
标签:请求,Send,handler,NET,HttpClient,Post,httpClient From: https://www.cnblogs.com/frankie-ming/p/17567435.html