/// <summary> /// 查询服务器返回的cookie值 /// </summary> /// <param name="cookieName"></param> /// <returns></returns> public string GetCookieValue(string cookieName) { Cookie c = Cookies.Find(k => k.Name == cookieName); if (c != null) { return(c.Value); } return(""); }
public Response GetResponse(int port = 80, int maxredirect = 10) { string content = Method.ToString() + " " + Uri.PathAndQuery + " HTTP/1.1\n"; List <string> headers = new List <string>(); if (!Headers.ContainsKey("User-Agent")) { Headers.Add("User-Agent", UserAgent); } else { Headers.Remove("User-Agent"); Headers.Add("User-Agent", UserAgent); } foreach (var p in Headers) { headers.Add(p.Key + ":" + p.Value + '\n'); } for (int i = 0; i < Cookies.Count; i++) { headers.Add("Cookie:" + Cookies[i] + '\n'); } if (referal != null) { headers.Add("Referer:" + referal + '\n'); } headers.Sort(StringComparer.Create(System.Threading.Thread.CurrentThread.CurrentCulture, true)); content += "Host:" + Uri.DnsSafeHost + '\n'; for (int i = 0; i < headers.Count; i++) { content += headers[i]; } if (((POSTContent ?? "") != "") && (Method == HttpRequestMethod.POST)) { content += "Content-Length:" + POSTContent.Length + '\n'; content += '\n' + POSTContent ?? ""; } else { content += "\n\n"; } System.Net.Sockets.TcpClient client; client = new System.Net.Sockets.TcpClient(); client.Connect(Uri.DnsSafeHost, port); var buf = Encoding.Default.GetBytes(content); client.GetStream().Write(buf, 0, buf.Length); var stream = client.GetStream(); List <byte> response = new List <byte>(); string respHeader = ""; int b = 0; bool endHeader = false; while ((b = stream.ReadByte()) != -1) { if (b == '\n') { if ((response.Count == 0) || ((response.Count == 1) && (response[0] == 13))) { if (!endHeader) { endHeader = true; response.Clear(); continue; } } response.Add((byte)b); if (!endHeader) { respHeader += Encoding.Default.GetString(response.ToArray()); response.Clear(); } } else { response.Add((byte)b); } } if (headers.Contains("Connection")) { if (Headers["Connection"] == "Close") { client.Close(); } } else { client.Close(); } string contentType = respHeader.Substring(respHeader.IndexOf("Content-Type:")); contentType = contentType.Substring(13, contentType.IndexOf('\n') - 13).Trim(); string responseContent = ""; if (contentType.IndexOf("text/") != -1) { string charset = (contentType.IndexOf("charset") != -1) ? contentType.Substring(contentType.IndexOf("charset") + 8) : "utf-8"; if (charset.IndexOf(";") != -1) { charset = charset.Substring(0, charset.IndexOf(';')); } responseContent = Encoding.GetEncoding(charset).GetString(response.ToArray()); } if ((AutoRedirect) && (maxredirect > 0)) { string result = respHeader.Substring(0, respHeader.IndexOf('\n')).Trim(); int sci = respHeader.IndexOf("\nSet-Cookie: "); while (sci != -1) { int eqi = respHeader.IndexOf('=', sci); string name = respHeader.Substring(sci + 13, eqi - sci - 13); string value = respHeader.Substring(eqi + 1, respHeader.IndexOf(';', sci) - eqi - 1); if (value != "deleted") { if (Cookies.FindIndex((x) => { return(x.Name == name); }) == -1) { Cookies.Add(new Cookie() { Name = name, Value = value }); } else { Cookies.Find((x) => { return(x.Name == name); }).Value = value; } } else { int index = Cookies.FindIndex((x) => { return(name == x.Name); }); if (index != -1) { Cookies.RemoveAt(index); } } sci = respHeader.IndexOf("\nSet-Cookie: ", sci + 1); } if (result == "HTTP/1.1 302 Found") { string redurl = respHeader.Substring(respHeader.IndexOf("\nLocation:")); redurl = redurl.Substring(redurl.IndexOf(':') + 1); redurl = redurl.Substring(0, redurl.IndexOf('\n')).Trim(); if (redurl == "/") { redurl = Uri.Scheme + "://" + Uri.Host + "/"; } Uri reuri = new Uri(redurl); return((new HttpRequest() { Uri = reuri, UserAgent = UserAgent, AutoRedirect = true, Headers = Headers, Cookies = Cookies, referal = Uri.DnsSafeHost + Uri.PathAndQuery }).GetResponse(port, maxredirect - 1)); } } return(new Response(respHeader, responseContent, Cookies.ToArray())); }