public void GetValues_InvalidSetCookieHeader_Success() { WebHeaderCollection w = new WebHeaderCollection(); w.Add(HeaderType, CookieInvalid); string[] values = w.GetValues(HeaderType); Assert.Equal(0, values.Length); }
public void GetValues_String_Success(string header, string value, string[] expectedValues) { WebHeaderCollection w = new WebHeaderCollection(); w.Add(header, value); string modifiedHeader = header.ToLowerInvariant(); // header should be case insensitive Assert.Equal(expectedValues, w.GetValues(modifiedHeader)); }
public void HttpRequestHeader_Get_Success() { WebHeaderCollection w = new WebHeaderCollection(); w.Add("header1", "value1"); w.Add("header1", "value2"); string[] values = w.GetValues(0); Assert.Equal("value1", values[0]); Assert.Equal("value2", values[1]); }
// Check for any icy related headers and add them to the metadata. private void LoadResponseHeaders(WebHeaderCollection whc) { MFError hrthrowonerror = SetString(ICY_HEADERS, whc.ToString()); for (int x = 0; x < whc.Count; x++) { string s = whc.Keys[x]; ProcessResponseHeader(s, whc.GetValues(x)); } }
public void HttpRequestHeader_Get_Success() { WebHeaderCollection w = new WebHeaderCollection(); w.Add("header1", "value1"); w.Add("header1", "value2"); string[] values = w.GetValues(0); Assert.Equal("value1", values[0]); Assert.Equal("value2", values[1]); }
public static Dictionary <string, IEnumerable <string> > ConvertHeadersToDictionary(WebHeaderCollection headers) { var retValue = new Dictionary <string, IEnumerable <string> >(); foreach (var key in headers.Keys) { retValue.Add(key.ToString(), new List <string>(headers.GetValues(key.ToString()))); } return(retValue); }
public void GetValues_Success() { WebHeaderCollection w = new WebHeaderCollection(); w.Add("Accept", "text/plain, text/html"); string[] values = w.GetValues("Accept"); Assert.Equal(2, values.Length); Assert.Equal("text/plain", values[0]); Assert.Equal("text/html", values[1]); }
public static string HttpHeaderToStr(string Method, string uri, WebHeaderCollection httpHeader, CookieCollection cookies) { // if (string.IsNullOrEmpty(httpHeader[HttpRequestHeader.Host])) // { int x = uri.IndexOf("http://"); if (x != -1) { x += 7; if (uri.IndexOf('/', x) == -1) { httpHeader[HttpRequestHeader.Host] = uri.Substring(x); uri = "/"; } else { string sx = uri.Substring(x, uri.IndexOf('/', x) - x); httpHeader[HttpRequestHeader.Host] = sx; uri = uri.Substring(sx.Length + 7); } } //} StringBuilder sb = new StringBuilder(); sb.Append(Method); sb.Append(" "); sb.Append(uri); sb.Append(" HTTP/1.1\r\n"); foreach (string h in httpHeader.AllKeys) { sb.Append(h + ": "); String[] values = httpHeader.GetValues(h); foreach (string v in values) { sb.Append(v + ","); } sb.Remove(sb.Length - 1, 1); sb.Append("\r\n"); } if (cookies.Count != 0) { sb.Append("Cookie: "); foreach (Cookie c in cookies) { sb.Append(c.Name); sb.Append("="); sb.Append(c.Value); sb.Append(";"); } sb.Remove(sb.Length - 1, 1); sb.Append("\r\n"); } sb.Append("\r\n"); return(sb.ToString()); }
private void SendRequest() { try { var req = (HttpWebRequest)WebRequest.Create(url); req.UserAgent = "Mozilla/5.0 (Windows NT 6.1; rv:46.0) Gecko/20100101 Firefox/46.0"; req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"; req.Headers.Add("Accept-Language", "en-US,en;q=0.5"); req.Method = (string.IsNullOrEmpty(textBoxMethod.Text)?"GET":textBoxMethod.Text); req.Timeout = 20000; var resp = (HttpWebResponse)req.GetResponse(); var m = new StreamReader(resp.GetResponseStream()).ReadToEnd(); this.Xframe = resp.Headers["X-Frame-Options"]; if (this.Xframe == "") { this.Xframe = resp.Headers["x-frame-options"]; } this.labelXframeValue.Text = Xframe; this.requestCompleted = true; this.statue = Xframe; WebHeaderCollection hd = resp.Headers; for (int i = 0; i < hd.Count; i++) { string header__ = hd.GetKey(i); string[] values = hd.GetValues(header__); if (values.Length > 0) { for (int j = 0; j < values.Length; j++) { if (!string.IsNullOrEmpty(values[j])) { this.headers += header__ + ":" + values[j] + "\r\n"; } } } else { this.headers += (header__ + ""); } } this.succeed = true; } catch (Exception s) { this.succeed = false; requestCompleted = true; bool _404 = s.Message.Contains("The remote server returned an error: (404) Not Found"); MessageBox.Show(s.Message + (_404?"\r\n can not check for headers in 404 response\r\n":""), "error on connection"); } labelFinalResult.Visible = labelXframeValue.Visible = xFrameOptionsToolStripMenuItem.Visible = currentHeadersToolStripMenuItem.Visible = succeed; labelerror.Visible = !succeed; }
public void GetValues_MultipleValuesHeader_Success() { WebHeaderCollection w = new WebHeaderCollection(); string headerType = "Accept"; w.Add(headerType, "text/plain, text/html"); string[] values = w.GetValues(headerType); Assert.Equal(2, values.Length); Assert.Equal("text/plain", values[0]); Assert.Equal("text/html", values[1]); }
public static void CopyHeadersFromHttpWebResponse(WebHeaderCollection webResponseHeaders, ResponseHeaders responseHeaders) { var keys = webResponseHeaders.Keys; for (int i = 0; i < keys.Count; ++i) { var k = keys[i]; var v = webResponseHeaders.GetValues(i); responseHeaders.Add(k, v); } }
public Dictionary <string, string> MapHeaders(WebHeaderCollection headerCollection) { var headers = new Dictionary <string, string>(); for (var i = 0; i < headerCollection.Count; i++) { headers.Add(headerCollection.GetKey(i), string.Join(",", headerCollection.GetValues(i))); } return(headers); }
/// <summary> /// handle response /// </summary> /// <param name="httpWebResponse"></param> /// <param name="response"></param> private static void HandleHttpWebResponse(HttpWebResponse httpWebResponse, Response response) { response.Code = (int)httpWebResponse.StatusCode; response.Message = httpWebResponse.StatusDescription; WebHeaderCollection headers = httpWebResponse.Headers; if (headers != null) { Dictionary <string, List <string> > result = new Dictionary <string, List <string> >(headers.Count); for (int i = 0; i < headers.Count; i++) { List <string> values = null; if (headers.GetValues(i) != null) { values = new List <string>(); foreach (string value in headers.GetValues(i)) { values.Add(value); } } result.Add(headers.GetKey(i), values); } response.Headers = result; } response.ContentLength = httpWebResponse.ContentLength; if (response.Body != null) { response.Body.ContentLength = httpWebResponse.ContentLength; response.Body.ContentType = httpWebResponse.ContentType; response.Body.OnRead(httpWebResponse.GetResponseStream()); } // print log printResponseInfo(httpWebResponse); httpWebResponse.Close(); // close }
public static Dictionary <string, string[]> ToDictionary(this WebHeaderCollection headers) { var dict = new Dictionary <string, string[]>(); for (var i = 0; i < headers.Count; ++i) { var header = headers.GetKey(i); dict.Add(header, headers.GetValues(i)); } return(dict); }
internal static Dictionary <string, List <string> > ToHeadersDictionary(this WebHeaderCollection headers) { var result = new Dictionary <string, List <string> >(StringComparer.OrdinalIgnoreCase); foreach (var header in headers.AllKeys) { var values = headers.GetValues(header); result.Add(header, new List <string>(values)); } return(result); }
private void PrintHeaders(WebHeaderCollection webHeaderCollection) { if (webHeaderCollection != null) { Trace.WriteLine("Headers:"); foreach (string key in webHeaderCollection.AllKeys) { string[] values = webHeaderCollection.GetValues(key); Trace.WriteLine(key + ": " + string.Join("; ", values.ToArray())); } } }
/// <summary> /// Get mode /// </summary> /// <param name="strUrl">完整的请求URL,包括参数串</param> /// <param name="Encoding">编码名称</param> /// <returns>返回请求状态描述</returns> public static string Get(string strUrl, string Encoding, WebHeaderCollection custHeaders) { string strRet = null; try { HttpWebRequest _WebReq = (HttpWebRequest)WebRequest.Create(strUrl); _WebReq.Timeout = MyTimeout; if (custHeaders != null) { for (int i = 0; i < custHeaders.Count; i++) { String header = custHeaders.GetKey(i); String[] values = custHeaders.GetValues(header); _WebReq.Headers.Add(header, String.Join(",", values)); } } if (string.IsNullOrEmpty(_WebReq.ContentType)) { _WebReq.ContentType = "application/x-www-form-urlencoded"; } HttpWebResponse response = null; try { response = (HttpWebResponse)_WebReq.GetResponse(); } catch (WebException ex) { response = (HttpWebResponse)ex.Response; } System.IO.Stream resStream = response.GetResponseStream(); Encoding encode = System.Text.Encoding.GetEncoding(Encoding); StreamReader readStream = new StreamReader(resStream, encode); Char[] read = new Char[256]; int count = readStream.Read(read, 0, 256); while (count > 0) { String str = new String(read, 0, count); strRet = strRet + str; count = readStream.Read(read, 0, 256); } resStream.Close(); } catch (Exception eee) { strRet = "期望:" + eee.Message; } return(strRet); }
public void GetValues() { WebHeaderCollection w = new WebHeaderCollection(); w.Add("Hello", "H1"); w.Add("Hello", "H2"); w.Add("Hello", "H3,H4"); string [] sa = w.GetValues("Hello"); Assert.AreEqual(3, sa.Length, "#1"); Assert.AreEqual("H1, H2,H3,H4", w.Get("Hello"), "#2"); w = new WebHeaderCollection(); w.Add("Accept", "H1"); w.Add("Accept", "H2"); w.Add("Accept", "H3,H4"); Assert.AreEqual(3, w.GetValues(0).Length, "#3a"); Assert.AreEqual(4, w.GetValues("Accept").Length, "#3b"); Assert.AreEqual("H1, H2,H3,H4", w.Get("Accept"), "#4"); w = new WebHeaderCollection(); w.Add("Allow", "H1"); w.Add("Allow", "H2"); w.Add("Allow", "H3,H4"); sa = w.GetValues("Allow"); Assert.AreEqual(4, sa.Length, "#5"); Assert.AreEqual("H1, H2,H3,H4", w.Get("Allow"), "#6"); w = new WebHeaderCollection(); w.Add("AUTHorization", "H1, H2, H3"); sa = w.GetValues("authorization"); Assert.AreEqual(3, sa.Length, "#9"); w = new WebHeaderCollection(); w.Add("proxy-authenticate", "H1, H2, H3"); sa = w.GetValues("Proxy-Authenticate"); Assert.AreEqual(3, sa.Length, "#9"); w = new WebHeaderCollection(); w.Add("expect", "H1,\tH2, H3 "); sa = w.GetValues("EXPECT"); Assert.AreEqual(3, sa.Length, "#10"); Assert.AreEqual("H2", sa [1], "#11"); Assert.AreEqual("H3", sa [2], "#12"); try { w.GetValues(null); Assert.Fail("#13"); } catch (ArgumentNullException) { } Assert.AreEqual(null, w.GetValues(""), "#14"); Assert.AreEqual(null, w.GetValues("NotExistent"), "#15"); }
static void Main(string[] args) { if (args.Length != 1) { Console.WriteLine("Need base path"); return; } string local = args[0]; string itemPath = local.Substring(local.IndexOf('/') + 1) + "/"; string picUrlBase = "https://crowd-media.loc.gov/" + itemPath.Substring(itemPath.IndexOf('/') + 1); Directory.CreateDirectory(itemPath); int iter = 1; while (true) { try { using (WebClient webClient = new WebClient()) { while (true) { string picurl = picUrlBase + iter.ToString() + ".jpg"; string localpath = itemPath + iter.ToString() + ".jpg"; //throw new Exception("manual fail"); webClient.DownloadFile(picurl, localpath); // Obtain the WebHeaderCollection instance containing the header name/value pair from the response. WebHeaderCollection webHeaderCollection = webClient.ResponseHeaders; long remoteFileSize = long.Parse(webHeaderCollection.GetValues("Content-Length")[0]); FileInfo localFile = new FileInfo(localpath); long localFileSize = localFile.Length; if (remoteFileSize == localFileSize) { break; } } } Console.WriteLine(picUrlBase + iter.ToString() + ".jpg"); iter++; } catch (Exception ex) { if (!ex.Message.Contains("404")) { Console.WriteLine(ex.Message); } break; } } Console.ReadKey(); }
void GetFile(object sender, System.ComponentModel.DoWorkEventArgs e) { object[] wrObj = wrStream(); if (wrObj.Length == 0) { return; } Stream wrData = (Stream)wrObj[1]; WebHeaderCollection wrHead = (WebHeaderCollection)wrObj[0]; if (wrHead.GetValues("Content-Length") == null) { iLen = 0; } else { iLen = Convert.ToInt32(wrHead.GetValues("Content-Length")[0]); } PadFile(sPath, iLen); FileStream fs = new FileStream(sPath, FileMode.Open, FileAccess.Write); fs.Seek(0, SeekOrigin.Begin); while (true) { byte[] b = new byte[8192]; int ib = wrData.Read(b, 0, b.Length); if (ib == 0) { break; } iRecv += ib; fs.Write(b, 0, ib); } fs.Flush(); fs.Close(); fs.Dispose(); wrData.Close(); wrData.Dispose(); State = eState.Completed; }
private int GetLocationNumber(WebHeaderCollection collection) { string[] k = collection.GetValues("Location"); if (k.Length > 0) { string line = k[0]; int pos = line.LastIndexOf('/'); string num = line.Substring(pos + 1); int ret = Int32.Parse(num); return(ret); } return(-1); }
private static void writeHeaders(StringBuilder builder, WebHeaderCollection headers) { foreach (string key in headers.AllKeys) { builder.Append(escape(key)); builder.Append(": "); string[] values = headers.GetValues(key).Select(v => escape(v)).ToArray(); string joinedValues = String.Join("; ", values); builder.Append(joinedValues); builder.Append(newLine); } builder.Append(newLine); }
public static IEnumerable <Tuple <string, string> > ToIEnumerable(this WebHeaderCollection headers) { if (headers == null) { return(null); } return(Enumerable .Range(0, headers.Count) .SelectMany(i => headers.GetValues(i) .Select(v => Tuple.Create(headers.GetKey(i), v)) )); }
public void GetValues_SingleSetCookieHeaderWithMultipleCookiesWithNoAttribute_Success() { WebHeaderCollection w = new WebHeaderCollection(); w.Add(HeaderType, Cookie1NoAttribute + "," + Cookie2NoAttribute + "," + Cookie3NoAttribute + "," + Cookie4NoAttribute); string[] values = w.GetValues(HeaderType); Assert.Equal(4, values.Length); Assert.Equal(Cookie1NoAttribute, values[0]); Assert.Equal(Cookie2NoAttribute, values[1]); Assert.Equal(Cookie3NoAttribute, values[2]); Assert.Equal(Cookie4NoAttribute, values[3]); }
private static void CopyHeadersFrom(this HttpResponseMessage message, WebHeaderCollection headers) { if (headers != null) { foreach (string headerName in headers) { string[] headerValues = headers.GetValues(headerName); if (!message.Headers.TryAddWithoutValidation(headerName, headerValues)) { message.Content.Headers.TryAddWithoutValidation(headerName, headerValues); } } } }
// Token: 0x0600008B RID: 139 RVA: 0x00003264 File Offset: 0x00001464 protected void LogInfoHeaders(WebHeaderCollection headers) { if (this.EasConnectionSettings.Log.IsEnabled(LogLevel.LogInfo)) { foreach (string text in headers.AllKeys) { this.EasConnectionSettings.Log.Info("{0}: {1}", new object[] { text, string.Join("|", headers.GetValues(text)) }); } } }
public static List <KeyValuePair <string, string> > ObtenerHeaderService(WebHeaderCollection headers) { List <KeyValuePair <string, string> > lstHeaders = new List <KeyValuePair <string, string> >(); int numFila = 0; foreach (string name in headers) { string[] dato = headers.GetValues(numFila); KeyValuePair <string, string> datoPar = new KeyValuePair <string, string>(name, dato[0]); lstHeaders.Add(datoPar); ++numFila; } return(lstHeaders); }
private void DebugLogHeaders(WebHeaderCollection headers, string label) { if (this.debug) { for (int i = 0; i < headers.Count; ++i) { string header = headers.GetKey(i); foreach (string value in headers.GetValues(i)) { DebugLog(label + " Header: " + header + " = " + value); } } } }
public CurlCmdArgumentsBuilder WithHeaderOutput() => WithArgument("--include"); // include response headers in output public CurlCmdArgumentsBuilder WithHeaders(WebHeaderCollection headers) { for (int i = 0; i < headers.Count; ++i) { string header = headers.GetKey(i); foreach (string value in headers.GetValues(i)) { AddArgument("--header", $"{header}: {headers[header]}"); } } return(this); }
private void button1_Click(object sender, EventArgs e) { // Создать объект запроса WebRequest request = WebRequest.Create(txb_url.Text); // Получить ответ с сервера WebResponse response = request.GetResponse(); // Получаем поток данных из ответа using (StreamReader stream = new StreamReader(response.GetResponseStream())) { // Выводим исходный код страницы string line; while ((line = stream.ReadLine()) != null) { txb_sourceCode.Text += line + System.Environment.NewLine; } } // Получаем некоторые данные о сервере string messageServer = "Целевой URL: \t" + request.RequestUri + System.Environment.NewLine + System.Environment.NewLine + "Метод запроса: \t" + request.Method + System.Environment.NewLine + "Тип полученных данных: \t" + response.ContentType + System.Environment.NewLine + "Длина ответа: \t" + response.ContentLength + System.Environment.NewLine + "Заголовки"; // Получаем заголовки, используем LINQ WebHeaderCollection whc = response.Headers; var headers = Enumerable.Range(0, whc.Count) .Select(p => { return(new { Key = whc.GetKey(p), Names = whc.GetValues(p) }); }); foreach (var item in headers) { messageServer += System.Environment.NewLine + item.Key + ":"; foreach (var n in item.Names) { messageServer += "\t" + n; } } txb_serverInfo.Text = messageServer; }
public string NotGet(string url, string data, string method, WebHeaderCollection headers) { Request = (HttpWebRequest)WebRequest.Create(url); Request.CookieContainer = CookieBox; byte[] buf = Encoding.UTF8.GetBytes(data); Request.Method = method; Request.ContentType = "application/x-www-form-urlencoded"; Request.ContentLength = buf.Length; Request.Accept = headers[HttpRequestHeader.Accept]; Request.Connection = headers[HttpRequestHeader.Connection]; Request.KeepAlive = Convert.ToBoolean(headers[HttpRequestHeader.KeepAlive]); Request.Referer = headers[HttpRequestHeader.Referer]; Request.UserAgent = headers[HttpRequestHeader.UserAgent]; var heads = new HttpRequestHeader[] { HttpRequestHeader.Accept, HttpRequestHeader.Connection, HttpRequestHeader.KeepAlive, HttpRequestHeader.Referer, HttpRequestHeader.UserAgent, HttpRequestHeader.ContentLength, HttpRequestHeader.ContentType }; var items = Enumerable .Range(0, headers.Count) .SelectMany(i => headers.GetValues(i) .Select(v => Tuple.Create(headers.GetKey(i), v))) .Where (x => !heads .Select(y => y.ToString()) .Contains(x.Item1) ) .ToList(); foreach (var item in items) { Request.Headers.Add(item.Item1, item.Item2); } using (var stream = Request.GetRequestStream()) stream.Write(buf, 0, data.Length); var response = (HttpWebResponse)Request.GetResponse(); if (response.Cookies != null && response.Cookies.Count != 0) { foreach (Cookie item in response.Cookies) { CookieBox.Add(item); } } return(new StreamReader(response.GetResponseStream()).ReadToEnd()); }
public void TestWebHeaderCollectionGetValues() { WebHeaderCollection whc = new WebHeaderCollection(); string[] strArray1; try { strArray1 = whc.GetValues(null); Fail("GetValues: failed to throw exception for null argument"); } catch(ArgumentNullException) { // Ok } whc.Add("phony:junk"); whc.Add("more", "stuff"); string[] strArray = whc.GetValues("phony"); if (strArray[0] != "junk") Fail("GetValues: returned incorrect data for 'phony:junk'"); strArray1 = whc.GetValues("more"); if (strArray1[0] != "stuff") Fail("GetValues: returned incorrect data for 'more:stuff'"); string[] strArray2 = whc.GetValues("notThere"); if (strArray2 != null) Fail("GetValues: did not return null for name:value not in collection"); }
public void HttpRequestHeader_GetValues_Success() { WebHeaderCollection w = new WebHeaderCollection(); w.Add("header1", "value1"); Assert.Equal("value1", w.GetValues("header1")[0]); }
private static void AddHeaderValues(WebHeaderCollection source, int index, string header, HttpHeaders destination) { string[] values = source.GetValues(index); // Even though AddWithoutValidation() could throw FormatException for header values containing newline // chars that are not followed by whitespace chars, we don't need to catch that exception. Our header // values were returned by HttpWebResponse which is trusted to only return valid header values. if (values.Length == 1) { destination.AddWithoutValidation(header, values[0]); } else { for (int j = 0; j < values.Length; j++) { destination.AddWithoutValidation(header, values[j]); } } }
public void GetValues_String_Success(string header, string value, string[] expectedValues) { WebHeaderCollection w = new WebHeaderCollection(); w.Add(header, value); string modifiedHeader = header.ToLowerInvariant(); // header should be case insensitive Assert.Equal(expectedValues, w.GetValues(modifiedHeader)); }
public void GetNonExistent_ReturnsNull_Success() { WebHeaderCollection w = new WebHeaderCollection(); Assert.Equal(0, w.Count); Assert.Null(w["name"]); Assert.Null(w.GetValues("name")); }
public void GetValues_Int_Success() { string[] keys = { "Accept", "uPgRaDe", "Custom" }; string[] values = { "text/plain, text/html", " HTTP/2.0 , SHTTP/1.3, , RTA/x11 ", "\"xyzzy\", \"r2d2xxxx\", \"c3piozzzz\"" }; WebHeaderCollection w = new WebHeaderCollection(); for (int i = 0; i < keys.Length; ++i) { w.Add(keys[i], values[i]); } for (int i = 0; i < keys.Length; ++i) { string[] expected = new[] { values[i].Trim() }; Assert.Equal(expected, w.GetValues(i)); } }
public void GetValues_Int_Fail() { WebHeaderCollection w = new WebHeaderCollection(); w.Add("Accept", "text/plain"); Assert.Throws<ArgumentOutOfRangeException>(() => w.GetValues(-1)); Assert.Throws<ArgumentOutOfRangeException>(() => w.GetValues(42)); }