public static async Task <CassetteRecordRequest> CreateFromRequest(HttpRequestMessage request, CookieContainer?cookieContainer) { var record = new CassetteRecordRequest(request.Method.Method, request.RequestUri, request.ToNameValueCollection()); // Host header is required by HTTP 1.1 spec, so we should add it if it is not provided if (record.Headers["Host"] == null) { record.Headers.Add("Host", request.RequestUri.IdnHost); } if (cookieContainer?.GetCookieHeader(request.RequestUri) is { } cookieHeader&& !string.IsNullOrWhiteSpace(cookieHeader)) { record.Headers.Add("Cookie", cookieHeader); } var(body, newContent) = await CassetteBody.CreateCassetteBody(request.Content); record.Body = body; if (newContent != null) { request.Content = newContent; } return(record); }
protected static HttpRequestMessage GetHttpRequest(HttpReq req, CookieContainer cc) { var request = new HttpRequestMessage(new HttpMethod(req.Method.ToString().ToUpper()), req.GetUrl()) { Content = new ByteArrayContent(req.GetBinaryData()) { Headers = { ContentType = MediaTypeHeaderValue.Parse(req.ContentType) } } }; foreach (var header in req.HeaderMap.Where(h => !_notAddHeaderNames.Contains(h.Key))) { request.Headers.Add(header.Key, header.Value); } var cookies = req.HeaderMap.GetOrDefault(HttpConstants.Cookie) ?? cc?.GetCookieHeader(request.RequestUri); if (!cookies.IsNullOrEmpty()) { request.Headers.Add(HttpConstants.Cookie, cookies); } return(request); }
private void SetCookies(CurlEasyHandle easyHandle) { string cookieHeader = CookieContainer?.GetCookieHeader(RequestUri); if (!string.IsNullOrEmpty(cookieHeader)) { LibCurl.EasySetOpt(easyHandle, CurlOption.Cookie, cookieHeader); } }
public static SortedDictionary<int, string> yt(string targetUri) { CookieContainer cc = new CookieContainer(); HttpWebRequest req = (HttpWebRequest)WebRequest.Create(targetUri); req.CookieContainer = cc; req.Timeout = 5000; req.GetResponse().Close(); req = (HttpWebRequest)WebRequest.Create("http://www.youtube.com/get_video_info?video_id=" + Regex.Match(targetUri, "(?<=v=)[\\w-]+").Value); req.CookieContainer = cc; string _info = null; System.Net.WebResponse res = req.GetResponse(); System.IO.StreamReader sr = new System.IO.StreamReader(res.GetResponseStream()); _info = sr.ReadToEnd(); sr.Close(); res.Close(); Hashtable info = new Hashtable(); Dictionary<string, string> _tmp = new Dictionary<string, string>(); SortedDictionary<int, string> fmtmap = new SortedDictionary<int, string>(); foreach (string item in _info.Split('&')) { info.Add(item.Split('=')[0], Uri.UnescapeDataString(item.Split('=')[1])); } if (Convert.ToString(info["status"]) == "fail") { throw new UnauthorizedAccessException(); } foreach (string item in Convert.ToString(info["url_encoded_fmt_stream_map"]).Split(',')) { foreach (string a in item.Split('&')) { _tmp.Add(a.Split('=')[0], Uri.UnescapeDataString(a.Split('=')[1])); } fmtmap.Add(Convert.ToInt32(_tmp["itag"]), (_tmp["url"]) + "&signature="); _tmp.Clear(); } req = (HttpWebRequest)WebRequest.Create("http://www.youtube.com/get_video_info?video_id=" + Regex.Match(targetUri, "(?<=v=)\\w+").Value + "&t=" + Convert.ToString(info["token"])); req.CookieContainer = cc; req.Timeout = 1500; req.GetResponse().Close(); fmtmap[-2] = Convert.ToString(info["title"]); fmtmap[-1] = cc.GetCookieHeader(new Uri("http://www.youtube.com")); info.Clear(); return fmtmap; }
public async Task FindById_when_anonymous_id_cookie_is_exists_should_return_user() { _mockAnonymousUserFactory.Setup(m => m.CreateAsync(It.IsAny <string>())).ReturnsAsync(new AnonymousUser() { Id = "test" }); var cookie = new Cookie(_anonOptions.CheckAnonymousIdCookieName, "test", "/", ".server"); var cookieContainer = new CookieContainer(); cookieContainer.Add(new Uri("http://server"), cookie); string cookieHeader = cookieContainer.GetCookieHeader(new Uri("http://server")); _mockHttpContextAccessor.Object.HttpContext.Request.Headers.Add("Cookie", cookieHeader); var user = await _subject.FindByIdAsync("test"); user.Id.Should().Be("test"); }
void SetCookies(OperationContext oc, CookieContainer cookieContainer) { HttpRequestMessageProperty httpRequestProperty = null; if (oc.OutgoingMessageProperties.ContainsKey(HttpRequestMessageProperty.Name)) { httpRequestProperty = oc.OutgoingMessageProperties[HttpRequestMessageProperty.Name] as HttpRequestMessageProperty; } if (httpRequestProperty == null) { httpRequestProperty = new HttpRequestMessageProperty(); oc.OutgoingMessageProperties.Add(HttpRequestMessageProperty.Name, httpRequestProperty); } httpRequestProperty.Headers.Add(HttpRequestHeader.Cookie, cookieContainer.GetCookieHeader(new Uri(@"http://someuri.tld"))); }
private static HttpWebRequest BuildRequest(HttpReq request, IWebProxyExt proxy, CookieContainer cc) { var req = (HttpWebRequest)WebRequest.Create(request.GetUrl()); req.AllowAutoRedirect = false; req.ContentType = request.ContentType; req.Method = request.Method.ToString().ToUpper(); req.Referer = request.Referrer; req.Host = request.Host; req.UserAgent = request.UserAgent; if (request.Timeout.HasValue) { req.Timeout = request.Timeout.Value; } if (proxy.Type != ProxyType.None) { req.Proxy = proxy; } else { req.Proxy = request.UseDefaultProxy ? WebRequest.DefaultWebProxy : null; } foreach (var header in request.HeaderMap.Where(h => !_notAddHeaderNames.Contains(h.Key))) { req.Headers.Add(header.Key, header.Value); } var cookies = request.HeaderMap.GetOrDefault(HttpConstants.Cookie) ?? cc?.GetCookieHeader(req.RequestUri); if (!cookies.IsNullOrEmpty()) { req.Headers.Add(HttpConstants.Cookie, cookies); } return(req); }
protected async Task <HttpResponseMessage> SendCookiesAsync(HttpRequestMessage request, CancellationToken cancellationToken) { if (AllowCookies) { string cookieHeader = _cookieContainer.GetCookieHeader(request.RequestUri); if (!string.IsNullOrEmpty(cookieHeader)) { request.Headers.Add("Cookie", cookieHeader); } } var response = await base.SendAsync(request, cancellationToken); if (AllowCookies && response.Headers.Contains("Set-Cookie")) { var responseCookieHeader = string.Join(",", response.Headers.GetValues("Set-Cookie")); _cookieContainer.SetCookies(request.RequestUri, responseCookieHeader); } return(response); }
protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { string cookieHeader = cookieContainer.GetCookieHeader(request.RequestUri); request.Headers.Add(HeaderNames.Cookie, cookieHeader); var response = await base.SendAsync(request, cancellationToken); if (response.Headers.TryGetValues(HeaderNames.SetCookie, out var values)) { foreach (var header in values) { // HACK: we cannot test on https so we have to force it to be insecure var keys = header.Split("; ").Where(x => !string.Equals(x, "secure")); var result = string.Join("; ", keys); cookieContainer.SetCookies(response.RequestMessage.RequestUri, result); } } return(response); }
public static HtmlResponse GetHtmlWebClient(string url, CookieContainer cc = null, string cookieHost = "", string host = "", string refere = "") { HtmlResponse res = new HtmlResponse { Success = false }; try { var wc = new WebClient(); if (cc != null) { wc.Headers.Add(HttpRequestHeader.Cookie, cc.GetCookieHeader(new Uri(cookieHost))); } wc.Headers.Add(HttpRequestHeader.UserAgent, string.Format(UserAgent, GetChromeVersion())); if (!string.IsNullOrEmpty(host)) { wc.Headers.Add(HttpRequestHeader.Host, host); } if (!string.IsNullOrEmpty(refere)) { wc.Headers.Add(HttpRequestHeader.Referer, refere); } var hltext = wc.DownloadData(url); res.Content = (Encoding.GetEncoding("UTF-8").GetString(hltext)); res.Success = true; } catch (Exception ex) { res.Content = ex.ToString(); } return(res); }
private async void CookieAwareWebClientAsync() { AccountCimage account = AppId.AccoutCimage; var url = new Uri("https://passport.csdn.net/account/login"); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "GET"; request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"; request.Headers["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36"; request.ContentType = "application/x-www-form-urlencoded"; CookieContainer cookie = new CookieContainer(); request.CookieContainer = cookie; HttpWebResponse response = (HttpWebResponse)(await request.GetResponseAsync()); cookie = request.CookieContainer; string sc = cookie.GetCookieHeader(url); string str = ""; using (StreamReader stream = new StreamReader(response.GetResponseStream())) { str = stream.ReadToEnd(); } Regex regex = new Regex(" type=\"hidden\" name=\"lt\" value=\"([\\w|\\-]+)\""); var lt = regex.Match(str).Groups[1].Value; regex = new Regex("type=\"hidden\" name=\"execution\" value=\"(\\w+)\""); var execution = regex.Match(str).Groups[1].Value; str = $"username={account.UserName}&password={account.Key}<={lt}&execution={execution}&_eventId=submit"; //str = str.Replace("@", "%40"); str = WebUtility.UrlEncode(str); request.Method = "post"; byte[] postby = Encoding.UTF8.GetBytes(str); }
//public CookieCollection GetCookieCollection(string url, string cookie) //{ // CookieCollection cookies = new CookieCollection(); // string cookiedomain = null; // Match match = Regex.Match(url, @"https*://([^\/]+)"); // if (match.Success) // { // cookiedomain = match.Groups[1].Value; // } // else // { // return cookies; // } // string[] cookstr = cookie.Split(';'); // foreach (string str in cookstr) // { // if (str.IndexOf("=") > 1) // { // string[] cookieNameValue = str.Split('='); // Cookie ck = new Cookie(cookieNameValue[0].Trim().ToString(), cookieNameValue[1].Trim().ToString()); // ck.Domain = cookiedomain;//必须写对 // cookies.Add(ck); // } // } // return cookies; //} /// <summary> /// /// </summary> /// <param name="url">登录地址</param> /// <param name="data">要提交的数据,可能会用到:HttpUtility.UrlEncode,str.Replace(" ","%20")</param> /// <param name="encoding">数据要进行什么编码,以及用什么编码返回</param> /// <param name="cookies"></param> /// <returns></returns> public static string Login(string url, string data, Encoding encoding, out string cookies, string referer = null, string userAgent = null) { HttpWebResponse response = null; try { var datas = encoding.GetBytes(data); var cookieContainer = new CookieContainer(); var request = WebRequest.Create(url) as HttpWebRequest; request.Method = "POST"; request.Accept = "*/*"; request.ContentType = "application/x-www-form-urlencoded"; request.UserAgent = userAgent; request.Referer = referer; request.KeepAlive = true; request.CookieContainer = cookieContainer; request.GetRequestStream().Write(datas, 0, datas.Length); response = (HttpWebResponse)request.GetResponse(); cookies = cookieContainer.GetCookieHeader(request.RequestUri); return(new StreamReader(response.GetResponseStream(), encoding).ReadToEnd()); } catch { cookies = null; return(null); } finally { if (response != null) { response.Close(); } } }
//************************************************************************ /// <summary> /// リクエスト送信時の処理 /// </summary> /// <param name="request">Message</param> /// <param name="channel">IClientChannel</param> /// <returns></returns> //************************************************************************ public object BeforeSendRequest(ref Message request, IClientChannel channel) { // ヘッダ情報の設定 request.Headers.Add(MessageHeader.CreateHeader("FormId", "ns", InformationManager.ClientInfo.FormId)); // クッキーコンテナを設定 //var cookieManager = channel.GetProperty<IHttpCookieContainerManager>(); //if (cookieManager != null) cookieManager.CookieContainer = s_cookieContainer; if (s_cookieContainer.Count > 0) { if (!request.Properties.ContainsKey(HttpRequestMessageProperty.Name)) { request.Properties.Add(HttpRequestMessageProperty.Name, new HttpRequestMessageProperty()); } var httpRequest = request.Properties[HttpRequestMessageProperty.Name] as HttpRequestMessageProperty; httpRequest.Headers[HttpRequestHeader.Cookie] = s_cookieContainer.GetCookieHeader(channel.RemoteAddress.Uri); } return(null); }
protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { CurrentUri = request.RequestUri; string cookieHeader = CookieContainer.GetCookieHeader(request.RequestUri); if (!string.IsNullOrEmpty(cookieHeader)) { request.Headers.Add("Cookie", cookieHeader); } var response = await base.SendAsync(request, cancellationToken); if (response.Headers.Contains("Set-Cookie")) { var responseCookieHeader = string.Join(",", response.Headers.GetValues("Set-Cookie")); CookieContainer.SetCookies(request.RequestUri, responseCookieHeader); } LastResponse = response; return(response); }
private async Task WriteRequestHeader(Stream stream, HttpRequestMessage request, HttpMethod requestMethod, Uri requestUri, CancellationToken cancellationToken) { using (var writer = new StringWriter { NewLine = "\r\n" }) { var pathAndQuery = requestUri.GetComponents(UriComponents.PathAndQuery, UriFormat.UriEscaped); writer.WriteLine("{1} {2} HTTP/{0}", request.Version ?? new Version(1, 1), requestMethod.Method, pathAndQuery); writer.WriteLine("Host: {0}", requestUri.Host); foreach (var header in request.Headers.Where(x => !string.Equals(x.Key, "Host", StringComparison.OrdinalIgnoreCase))) { writer.WriteLine("{0}: {1}", header.Key, string.Join(",", header.Value)); } //foreach (var headerValue in header.Value) // writer.WriteLine("{0}: {1}", header.Key, headerValue); if (request.Content != null) { foreach (var header in request.Content.Headers) { writer.WriteLine("{0}: {1}", header.Key, string.Join(",", header.Value)); } } if (UseCookies && CookieContainer != null) { var cookieHeader = CookieContainer.GetCookieHeader(request.RequestUri); if (!string.IsNullOrEmpty(cookieHeader)) { writer.WriteLine("Cookie: {0}", cookieHeader); } } writer.WriteLine(); var encoding = new UTF8Encoding(false); var data = encoding.GetBytes(writer.ToString()); await stream.WriteAsync(data, 0, data.Length, cancellationToken); } }
public async Task GetFileDownload(Uri uri, Uri referer, string outFile) { _logger.LogInformation($"Attempting to download file from {uri}"); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, uri); request.Headers.Add("Referer", referer.ToString()); Uri downloadUri = uri; HttpResponseMessage response = await _client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead); if (response.StatusCode == HttpStatusCode.Found) { _logger.LogInformation("Redirect found. Attempting to follow."); string newDownloadLocation = response.Headers.GetValues("Location").First(); if (string.IsNullOrEmpty(newDownloadLocation)) { _logger.LogError($"Failed to download file!"); throw new Exception("Failed to download file!"); } downloadUri = new Uri(newDownloadLocation); } else if (!response.IsSuccessStatusCode) { _logger.LogError("Download call failed."); throw new Exception("Download call failed."); } WebClient downloadClient = new WebClient(); downloadClient.Headers.Add(HttpRequestHeader.Cookie, _cookieContainer.GetCookieHeader(uri)); downloadClient.Headers.Add(HttpRequestHeader.Referer, referer.ToString()); downloadClient.DownloadFile(downloadUri, outFile); }
public TrackemonSession FindSessionId() { var trackemonSession = new TrackemonSession(); try { var cookieContainer = new CookieContainer(); const string homepageUrl = "https://www.trackemon.com"; var request = WebRequest.CreateHttp(homepageUrl); request.Method = "GET"; request.Timeout = Timeout; request.CookieContainer = cookieContainer; using (var response = request.GetResponse()) { var cookieHeader = cookieContainer.GetCookieHeader(new Uri("https://www.trackemon.com")); trackemonSession.cookieHeader = cookieHeader; using (var reader = new StreamReader(response.GetResponseStream())) { string line; while ((line = reader.ReadLine()) != null) { var match = Regex.Match(line, @"var\s+sessionId\s*=\s*\'(1?.*)\'\s*;"); if (match.Success) { trackemonSession.sessionId = match.Groups[1].Value; return(trackemonSession); } } } } } catch (Exception e) { Log.Debug("Error trying to get a sessionId for Trackemon: {0}", e.Message); } return(null); }
public static void Main(string[] args) { Uri uri = new Uri("http://myemsl-dev5.emsl.pnl.gov/"); CookieContainer cc = new CookieContainer(); Authenticate a = new Authenticate(); int res; Console.WriteLine("T1a"); res = a.process(cc); if (res == 0) { Console.WriteLine("Cookies {0}", cc.GetCookieHeader(uri)); Console.WriteLine("Cookie Count {0} {1}", cc.GetCookies(uri).Count, cc.Capacity); foreach (Cookie cookie in cc.GetCookies(uri)) { Console.WriteLine("Cookie {0}", cookie); } test_auth(new Uri("http://myemsl-dev5.emsl.pnl.gov/myemsl/testauth/"), cc); logout(new Uri("http://myemsl-dev5.emsl.pnl.gov/myemsl/logout/"), cc); } Console.WriteLine("T2"); Console.WriteLine(res); }
public async static Task <string> GetOneOneFiveContent(string url) { string ret = ""; var cookie = await Get115Cookie(); CookieContainer cc = cookie.Item1; var ccStr = ""; if (cc.Count > 0) { ccStr = cc.GetCookieHeader(new Uri(OneOneFiveCookieHost)); } using (HttpClient client = new()) { client.DefaultRequestHeaders.Add("Cookie", ccStr); client.DefaultRequestHeaders.Add("User-Agent", cookie.Item2); ret = await client.GetStringAsync(url); } return(ret); }
protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken ct) { var requestUri = request.RequestUri; request.Headers.Add(HeaderNames.Cookie, _cookieContainer.GetCookieHeader(requestUri)); var response = await base.SendAsync(request, ct); if (response.Headers.TryGetValues(HeaderNames.SetCookie, out IEnumerable <string> setCookieHeaders)) { foreach (var cookieHeader in SetCookieHeaderValue.ParseList(setCookieHeaders.ToList())) { Cookie cookie = new Cookie(cookieHeader.Name.Value, cookieHeader.Value.Value, cookieHeader.Path.Value); if (cookieHeader.Expires.HasValue) { cookie.Expires = cookieHeader.Expires.Value.DateTime; } _cookieContainer.Add(requestUri, cookie); } } return(response); }
private void btnSend_Click(object sender, EventArgs e) { if (ckbxKeepCookie.Checked) { httper.Cookie = this.cookie; wbMain.Document.Cookie = cookie.GetCookieHeader(new Uri(cbxUrl.Text.Trim())); } else { httper.Cookie = new CookieContainer(); wbMain.Document.Cookie = ""; } if (ckbxUserWebBrowser.Checked) { wbMain.Navigate(cbxUrl.Text.Trim()); } else { httper.Url = cbxUrl.Text.Trim(); httper.Referer = httper.Url; httper.Charset = cbxPageCharset.Text.Trim(); ShowMessage(""); ShowMessage(""); ShowMessage("BEGIN REQUEST:"); ShowCookie(); if (ckbxIsPost.Checked) { httper.PostData = tbxPostData.Text.Trim();// HttpUtility.UrlEncode("", Encoding.GetEncoding(cbxPageCharset.Text.Trim())); httper.RequestStringAsync(EnumRequestMethod.POST); } else { httper.RequestStringAsync(EnumRequestMethod.GET); } } }
private static async Task <(bool, string)> ParseNetscape(TextReader tr) { var cc = new CookieContainer(); string line; while ((line = await tr.ReadLineAsync()) != null) { if (string.IsNullOrWhiteSpace(line) || line.StartsWith("#")) { continue; } try { var s = line.Split('\t'); // Uri flag Path Secure expire name value // 0 1 2 3 4 5 6 // .twitter.com TRUE / FALSE 1634351167 des_opt_in N var cookie = new Cookie(s[5], s[6], s[2], s[0]) { //Discard = s[1] == "TRUE", Secure = s[3] == "TRUE", Expires = DateTime.MaxValue, }; cc.Add(cookie); } catch { } } var cookieStr = cc.GetCookieHeader(TwitterUri); return(!string.IsNullOrWhiteSpace(cookieStr), cookieStr); }
/// <summary> /// 获取一个网页的内容的GET方法 /// </summary> /// <param name="url">请求的URL</param> /// <param name="accept">Accept</param> /// <param name="encoding">设置响应编码</param> /// <param name="referer">Referer</param> /// <param name="language">Language</param> /// <param name="cc">Cookie</param> /// <param name="contentType">contentType</param> /// <param name="returnUrl">跳转URL</param> /// <returns>返回请求的数据</returns> public static string GetHtml(string url, string accept, string contentType, string referer, string language, CookieContainer cc, Encoding encoding, out string returnUrl) { HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url); req.CookieContainer = cc; req.Accept = accept; req.ContentType = contentType; if (!string.IsNullOrEmpty(referer)) { req.Referer = referer; } req.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; SLCC1; .NET CLR 2.0.50727; InfoPath.2; .NET CLR 3.0.04506; .NET CLR 3.5.21022)"; if (!string.IsNullOrEmpty(language)) { req.Headers.Add("Accept-Language", language); } req.Headers.Add("UA-CPU", "x86"); req.AllowAutoRedirect = true; req.KeepAlive = true; req.Method = "GET"; req.UseDefaultCredentials = true; using (var res = (HttpWebResponse)req.GetResponse()) { var response = res.GetResponseStream(); if (req.CookieContainer.IsNotNull()) { res.Cookies = req.CookieContainer.GetCookies(req.RequestUri); string cks = cc.GetCookieHeader(req.RequestUri); Debug.WriteLine(cks); } string responseUrl = res.ResponseUri.ToString(); returnUrl = responseUrl; using (StreamReader sr = new StreamReader(response, encoding)) { string content = sr.ReadToEnd(); return(content); } } }
public void Test3() { // var q = QRCodeHelper.EncodeQrCode2("https://www.fczbl.vip/",20,3); //string url = "https://tutut.ml/tool/free_ssr"; //Hashtable headerItem = new Hashtable(); //var cookies = HttpHelper.GetHttpWebRequest(url, "utf-8", headerItem, cookies: default); //url = "https://tutut.ml/tool/api/free_ssr?page=1&limit=10"; //string html = HttpHelper.GetHttpWebRequest(url, cookie: "__cfduid=dfe25745309f7c3c2116016a77e9ef8d81574224344; JSESSIONID=E0E81E4F56B0241C5CA809A3E209F106;"); //var i = ImageHelper.GetImage("d:/下载.png"); //var ss = QRCodeHelper.DecodeQrCode((Bitmap)i); //System.Diagnostics.Debug.WriteLine(ss); CookieContainer cookieContainer = new CookieContainer(); var handler = new HttpClientHandler(); handler.CookieContainer = cookieContainer; handler.UseCookies = true; using (var client = new HttpClient(handler)) { client.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36"); var uri = new Uri("https://tutut.ml/tool/free_ssr"); var message = new HttpRequestMessage(HttpMethod.Get, uri); var v = client.SendAsync(message).Result; var cookies = cookieContainer.GetCookieHeader(uri); uri = new Uri("https://tutut.ml/tool/api/free_ssr?page=1&limit=10"); message = new HttpRequestMessage(HttpMethod.Get, uri); message.Headers.Add("Cookie", cookies); var result = client.SendAsync(message).Result; var cc = result.Content.ReadAsStringAsync().Result; } }
private bool unJoin2(string comId, CookieContainer cc, MainForm form, config.config cfg) { util.debugWriteLine("follow user2 " + comId); try { var url = "https://public.api.nicovideo.jp/v1/user/followees/niconico-users/" + comId + ".json"; var headers = new Dictionary <string, string> { { "Content-Type", "application/json" }, { "X-Frontend-Id", "6" }, { "X-Frontend-Version", "0" }, { "X-Request-With", "https://www.nicovideo.jp/user/" + comId }, { "Cookie", cc.GetCookieHeader(new Uri(url)) }, }; var res = util.sendRequest(url, headers, null, "DELETE", false); using (var r = res.GetResponseStream()) using (var sr = new StreamReader(r)) { var rr = sr.ReadToEnd(); util.debugWriteLine("unjoin2 res " + rr); return(rr.IndexOf("\"status\":200") > -1); } } catch (Exception e) { util.debugWriteLine(e.Message + e.Source + e.StackTrace + e.TargetSite); return(false); } }
String GetCookie(Uri uri, String cookieName) { int datasize = 0; StringBuilder cookieData = new StringBuilder(datasize); InternetGetCookieEx(uri.ToString(), cookieName, cookieData, ref datasize, InternetCookieHttponly, IntPtr.Zero); if (Marshal.GetLastWin32Error() == ERROR_INSUFFICIENT_BUFFER && datasize > 0) { cookieData = new StringBuilder(datasize); if (InternetGetCookieEx(uri.ToString(), cookieName, cookieData, ref datasize, InternetCookieHttponly, IntPtr.Zero)) { if (cookieData.Length > 0) { CookieContainer container = new CookieContainer(); container.SetCookies(uri, cookieData.ToString()); return(container.GetCookieHeader(uri)); } } } return(String.Empty); }
public static void GetCookieHeader_Invalid() { CookieContainer cc = new CookieContainer(); Assert.Throws<ArgumentNullException>(() => cc.GetCookieHeader(null)); }
async Task <HttpURLConnection> SetupRequestInternal(HttpRequestMessage request, URLConnection conn) { if (conn == null) { throw new ArgumentNullException(nameof(conn)); } var httpConnection = conn.JavaCast <HttpURLConnection> (); if (httpConnection == null) { throw new InvalidOperationException($"Unsupported URL scheme {conn.URL.Protocol}"); } httpConnection.RequestMethod = request.Method.ToString(); // SSL context must be set up as soon as possible, before adding any content or // headers. Otherwise Java won't use the socket factory SetupSSL(httpConnection as HttpsURLConnection); if (request.Content != null) { AddHeaders(httpConnection, request.Content.Headers); } AddHeaders(httpConnection, request.Headers); List <string> accept_encoding = null; decompress_here = false; if ((AutomaticDecompression & DecompressionMethods.GZip) != 0) { AppendEncoding(GZIP_ENCODING, ref accept_encoding); decompress_here = true; } if ((AutomaticDecompression & DecompressionMethods.Deflate) != 0) { AppendEncoding(DEFLATE_ENCODING, ref accept_encoding); decompress_here = true; } if (AutomaticDecompression == DecompressionMethods.None) { accept_encoding?.Clear(); AppendEncoding(IDENTITY_ENCODING, ref accept_encoding); // Turns off compression for the Java client } if (accept_encoding?.Count > 0) { httpConnection.SetRequestProperty("Accept-Encoding", String.Join(",", accept_encoding)); } if (UseCookies && CookieContainer != null) { string cookieHeaderValue = CookieContainer.GetCookieHeader(request.RequestUri); if (!String.IsNullOrEmpty(cookieHeaderValue)) { httpConnection.SetRequestProperty("Cookie", cookieHeaderValue); } } HandlePreAuthentication(httpConnection); await SetupRequest(request, httpConnection).ConfigureAwait(continueOnCapturedContext: false);; SetupRequestBody(httpConnection, request); return(httpConnection); }
public string GetWebData(string url, string postData = null, CookieContainer cookies = null, string referer = null, IWebProxy proxy = null, bool forceUTF8 = false, bool allowUnsafeHeader = false, string userAgent = null, Encoding encoding = null, NameValueCollection headers = null, bool cache = true, string requestMethod = null, string contentType = null) { requestMethod = string.IsNullOrEmpty(requestMethod) ? ((postData == null) ? "GET" : "POST") : requestMethod; // do not use the cache when not doing a GET if (requestMethod != "GET") { cache = false; } // set a few headers if none were given if (headers == null) { headers = new NameValueCollection(); headers.Add("Accept", "*/*"); // accept any content type headers.Add("User-Agent", userAgent ?? OnlineVideoSettings.Instance.UserAgent); // set the default OnlineVideos UserAgent when none specified } if (referer != null) { headers.Set("Referer", referer); } HttpWebResponse response = null; try { // build a CRC of the url and all headers + proxy + cookies for caching string requestCRC = Helpers.EncryptionUtils.CalculateCRC32( string.Format("{0}{1}{2}{3}", url, headers != null ? string.Join("&", (from item in headers.AllKeys select string.Format("{0}={1}", item, headers[item])).ToArray()) : "", proxy != null ? proxy.GetProxy(new Uri(url)).AbsoluteUri : "", cookies != null ? cookies.GetCookieHeader(new Uri(url)) : "")); // try cache first string cachedData = cache ? WebCache.Instance[requestCRC] : null; Log.Debug("GetWebData-{2}{1}: '{0}'", url, cachedData != null ? " (cached)" : "", requestMethod); if (cachedData != null) { return(cachedData); } // build the request if (allowUnsafeHeader) { Helpers.DotNetFrameworkHelper.SetAllowUnsafeHeaderParsing(true); } HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; if (request == null) { return(""); } request.Method = requestMethod; request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; // turn on automatic decompression of both formats (adds header "AcceptEncoding: gzip,deflate" to the request) if (cookies != null) { request.CookieContainer = cookies; // set cookies if given } if (proxy != null) { request.Proxy = proxy; // send the request over a proxy if given } if (postData != null) { contentType = string.IsNullOrEmpty(contentType) ? "application/x-www-form-urlencoded" : contentType; } if (!string.IsNullOrEmpty(contentType)) { ((WebRequest)request).ContentType = contentType; } if (headers != null) // set user defined headers { foreach (var headerName in headers.AllKeys) { switch (headerName.ToLowerInvariant()) { case "accept": request.Accept = headers[headerName]; break; case "user-agent": request.UserAgent = headers[headerName]; break; case "referer": request.Referer = headers[headerName]; break; default: request.Headers.Set(headerName, headers[headerName]); break; } } } if (postData != null) { byte[] data = encoding != null?encoding.GetBytes(postData) : Encoding.UTF8.GetBytes(postData); request.ContentLength = data.Length; request.ProtocolVersion = HttpVersion.Version10; Stream requestStream = request.GetRequestStream(); requestStream.Write(data, 0, data.Length); requestStream.Close(); } // request the data try { response = (HttpWebResponse)request.GetResponse(); } catch (WebException webEx) { Log.Debug(webEx.Message); response = (HttpWebResponse)webEx.Response; // if the server returns a 404 or similar .net will throw a WebException that has the response } Stream responseStream = response.GetResponseStream(); // UTF8 is the default encoding as fallback Encoding responseEncoding = Encoding.UTF8; // try to get the response encoding if one was specified and neither forceUTF8 nor encoding were set as parameters if (!forceUTF8 && encoding == null && response.CharacterSet != null && !String.IsNullOrEmpty(response.CharacterSet.Trim())) { responseEncoding = Encoding.GetEncoding(response.CharacterSet.Trim(new char[] { ' ', '"' })); } // the caller did specify a forced encoding if (encoding != null) { responseEncoding = encoding; } // the caller wants to force UTF8 if (forceUTF8) { responseEncoding = Encoding.UTF8; } using (StreamReader reader = new StreamReader(responseStream, responseEncoding, true)) { string str = reader.ReadToEnd().Trim(); // add to cache if HTTP Status was 200 and we got more than 500 bytes (might just be an errorpage otherwise) if (cache && response.StatusCode == HttpStatusCode.OK && str.Length > 500) { WebCache.Instance[requestCRC] = str; } return(str); } } finally { if (response != null) { ((IDisposable)response).Dispose(); } // disable unsafe header parsing if it was enabled if (allowUnsafeHeader) { Helpers.DotNetFrameworkHelper.SetAllowUnsafeHeaderParsing(false); } } }
public HttpResponse GetResponse(HttpRequest request, CookieContainer cookies) { if (!CheckAvailability()) { throw new ApplicationException("Curl failed to initialize."); } lock (CurlGlobalHandle.Instance) { Stream responseStream = new MemoryStream(); Stream headerStream = new MemoryStream(); using (var curlEasy = new CurlEasy()) { curlEasy.AutoReferer = false; curlEasy.WriteFunction = (b, s, n, o) => { responseStream.Write(b, 0, s * n); return(s * n); }; curlEasy.HeaderFunction = (b, s, n, o) => { headerStream.Write(b, 0, s * n); return(s * n); }; AddProxy(curlEasy, request); curlEasy.Url = request.Url.FullUri; switch (request.Method) { case HttpMethod.GET: curlEasy.HttpGet = true; break; case HttpMethod.POST: curlEasy.Post = true; break; case HttpMethod.PUT: curlEasy.Put = true; break; default: throw new NotSupportedException(string.Format("HttpCurl method {0} not supported", request.Method)); } curlEasy.UserAgent = request.UseSimplifiedUserAgent ? UserAgentBuilder.UserAgentSimplified : UserAgentBuilder.UserAgent;; curlEasy.FollowLocation = request.AllowAutoRedirect; if (request.RequestTimeout != TimeSpan.Zero) { curlEasy.Timeout = (int)Math.Ceiling(request.RequestTimeout.TotalSeconds); } if (OsInfo.IsWindows) { curlEasy.CaInfo = _caBundleFilePath; } if (cookies != null) { curlEasy.Cookie = cookies.GetCookieHeader((Uri)request.Url); } if (request.ContentData != null) { curlEasy.PostFieldSize = request.ContentData.Length; curlEasy.SetOpt(CurlOption.CopyPostFields, new string(Array.ConvertAll(request.ContentData, v => (char)v))); } // Yes, we have to keep a ref to the object to prevent corrupting the unmanaged state using (var httpRequestHeaders = SerializeHeaders(request)) { curlEasy.HttpHeader = httpRequestHeaders; var result = curlEasy.Perform(); if (result != CurlCode.Ok) { switch (result) { case CurlCode.SslCaCert: case (CurlCode)77: throw new WebException(string.Format("Curl Error {0} for Url {1}, issues with your operating system SSL Root Certificate Bundle (ca-bundle).", result, curlEasy.Url)); default: throw new WebException(string.Format("Curl Error {0} for Url {1}", result, curlEasy.Url)); } } } var webHeaderCollection = ProcessHeaderStream(request, cookies, headerStream); var responseData = ProcessResponseStream(request, responseStream, webHeaderCollection); var httpHeader = new HttpHeader(webHeaderCollection); return(new HttpResponse(request, httpHeader, responseData, (HttpStatusCode)curlEasy.ResponseCode)); } } }
public static void DumpToJson(this CookieContainer cookies, Uri uri, JToken json) { json["cookie_header"] = cookies.GetCookieHeader(uri); }
public HttpResponse GetResponse(HttpRequest request, CookieContainer cookies) { if (!CheckAvailability()) { throw new ApplicationException("Curl failed to initialize."); } if (request.NetworkCredential != null) { throw new NotImplementedException("Credentials not supported for curl dispatcher."); } lock (CurlGlobalHandle.Instance) { Stream responseStream = new MemoryStream(); Stream headerStream = new MemoryStream(); using (var curlEasy = new CurlEasy()) { curlEasy.AutoReferer = false; curlEasy.WriteFunction = (b, s, n, o) => { responseStream.Write(b, 0, s * n); return(s * n); }; curlEasy.HeaderFunction = (b, s, n, o) => { headerStream.Write(b, 0, s * n); return(s * n); }; curlEasy.Url = request.Url.AbsoluteUri; switch (request.Method) { case HttpMethod.GET: curlEasy.HttpGet = true; break; case HttpMethod.POST: curlEasy.Post = true; break; case HttpMethod.PUT: curlEasy.Put = true; break; default: throw new NotSupportedException(string.Format("HttpCurl method {0} not supported", request.Method)); } curlEasy.UserAgent = UserAgentBuilder.UserAgent; curlEasy.FollowLocation = request.AllowAutoRedirect; if (OsInfo.IsWindows) { curlEasy.CaInfo = "curl-ca-bundle.crt"; } if (cookies != null) { curlEasy.Cookie = cookies.GetCookieHeader(request.Url); } if (!request.Body.IsNullOrWhiteSpace()) { // TODO: This might not go well with encoding. curlEasy.PostFieldSize = request.Body.Length; curlEasy.SetOpt(CurlOption.CopyPostFields, request.Body); } // Yes, we have to keep a ref to the object to prevent corrupting the unmanaged state using (var httpRequestHeaders = SerializeHeaders(request)) { curlEasy.HttpHeader = httpRequestHeaders; var result = curlEasy.Perform(); if (result != CurlCode.Ok) { throw new WebException(string.Format("Curl Error {0} for Url {1}", result, curlEasy.Url)); } } var webHeaderCollection = ProcessHeaderStream(request, cookies, headerStream); var responseData = ProcessResponseStream(request, responseStream, webHeaderCollection); var httpHeader = new HttpHeader(webHeaderCollection); return(new HttpResponse(request, httpHeader, responseData, (HttpStatusCode)curlEasy.ResponseCode)); } } }
/// <summary> /// Logs the user in /// </summary> /// <param name="user">Reddit account username</param> /// <param name="pswd">Reddit account password</param> /// <returns>True/False depending on success of login</returns> public bool Login(string user, string pswd) { string postData = string.Format("api_type=json&user={0}&passwd={1}", user, pswd); string loginURI = m_domain + string.Format(APIPaths.login, user); Hashtable response = SendPOST(postData, loginURI); m_usr = user; m_errors = GetErrorsFromRedditJson(response); //First check for errors. Should always contain errors key. if (m_errors != "" ) { return false; } //Only need the data segment Hashtable data = ((Hashtable)((Hashtable)response["json"])["data"]); m_modhash = data["modhash"].ToString(); string cookieval = data["cookie"].ToString(); redditCookie = new CookieContainer(); Uri cookieuri = new Uri(m_domain + string.Format(APIPaths.login, user)); redditCookie.Add(cookieuri, new Cookie("reddit_session", cookieval.Replace(",", "%2c").Replace(":", "%3A"), "/", "reddit.com")); jsonGet.Headers["cookie"] = redditCookie.GetCookieHeader(cookieuri); jsonGet.Headers["Useragent"] = m_useragent; m_logged_in = true; return true; }
private static string GetCookieHeader(Uri uri, CookieContainer cookies) { string cookieHeader = null; Debug.Assert(cookies != null); string cookieValues = cookies.GetCookieHeader(uri); if (!string.IsNullOrEmpty(cookieValues)) { cookieHeader = string.Format(CultureInfo.InvariantCulture, "{0}: {1}", HeaderNameCookie, cookieValues); } return cookieHeader; }
public static string GetCookieHeader(Uri uri, CookieContainer cookies) { string cookieHeader = null; Debug.Assert(cookies != null); string cookieValues = cookies.GetCookieHeader(uri); if (!string.IsNullOrEmpty(cookieValues)) { cookieHeader = CookieHeaderNameWithColon + " " + cookieValues; } return cookieHeader; }