public string Post(string url, string sPostString, string encode, out string sHeaders) { HttpSocket.UrlInfo urlInfo = this.ParseURL(url); string text = ""; if (urlInfo.Body != "") { text += string.Format("POST {0}?{1} HTTP/1.1\r\n", urlInfo.File, urlInfo.Body); } else { text += string.Format("POST {0} HTTP/1.1\r\n", urlInfo.File); } text += string.Format("Host:{0}:{1}\r\n", urlInfo.Host, urlInfo.Port.ToString()); text += string.Format("Content-Length:{0}\r\n", sPostString.Length.ToString()); text += string.Format("Content-Type:{0}\r\n", this.ContentType); text += string.Format("Referer:{0}\r\n", this.sReferer); text += "Accept: image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/msword, */*\r\n"; text += "Accept-Language:zh-CN\r\n"; text += string.Format("User-Agent:{0}\r\n", this.sUserAgent); text += string.Format("Connection:Close\r\n", new object[0]); text += "Cache-Control: no-cache\r\n"; text += string.Format("Cookie:{0}", this.sCookies); text += string.Format("\r\n\r\n", new object[0]); text += string.Format("{0}", sPostString); return(this.GetResponse(urlInfo.Host, urlInfo.Port, text, encode, out sHeaders)); }
private HttpSocket.UrlInfo ParseURL(string url) { if (!url.ToLower().StartsWith("http://")) { url = "http://" + url; } HttpSocket.UrlInfo result = default(HttpSocket.UrlInfo); result.Host = ""; result.Port = 80; result.File = "/"; result.Body = ""; int num = url.ToLower().IndexOf("http://"); if (num != -1) { url = url.Substring(7); num = url.IndexOf("/"); if (num == -1) { result.Host = url; } else { result.Host = url.Substring(0, num); url = url.Substring(num); num = result.Host.IndexOf(":"); if (num != -1) { string[] array = result.Host.Split(new char[] { ':' }); result.Host = array[0]; int.TryParse(array[1], out result.Port); } num = url.IndexOf("?"); if (num == -1) { result.File = url; } else { string[] array = url.Split(new char[] { '?' }); result.File = array[0]; result.Body = array[1]; } } } return(result); }
public bool DownloadFile2(string sFileUrl, string sFilePath, out string sMsg) { sMsg = ""; bool result; if (sFileUrl == "" || sFilePath == "") { sMsg = "文件URL和文件保存路径都不能为空"; result = false; } else { string path = sFilePath.Remove(sFilePath.LastIndexOf('\\')); if (Directory.Exists(path)) { if (File.Exists(sFilePath)) { File.Delete(sFilePath); } } else { Directory.CreateDirectory(path); } HttpSocket.UrlInfo urlInfo = this.ParseURL(sFileUrl); string text; if (urlInfo.Body != "") { text = string.Format("GET {0}?{1} HTTP/1.1\r\n", urlInfo.File, urlInfo.Body); } else { text = string.Format("GET {0} HTTP/1.1\r\n", urlInfo.File); } if (urlInfo.Port == 80) { text += string.Format("Host:{0}\r\n", urlInfo.Host, urlInfo.ToString()); } else { text += string.Format("Host:{0}:{1}\r\n", urlInfo.Host, urlInfo.Port.ToString()); } text += string.Format("Referer:{0}\r\n", this.sReferer); text += string.Format("User-Agent:{0}\r\n", this.sUserAgent); text += string.Format("Connection:Close\r\n", new object[0]); text += string.Format("Cookie:{0}", this.sCookies); text += "\r\n\r\n"; text += "OK"; string text2 = string.Empty; byte[] bytes = System.Text.Encoding.ASCII.GetBytes(text); byte[] array = new byte[1]; using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) { try { socket.Connect(urlInfo.Host, urlInfo.Port); if (socket.Connected) { socket.Send(bytes, bytes.Length, SocketFlags.None); int count; while ((count = socket.Receive(array, array.Length, SocketFlags.None)) > 0) { text2 += System.Text.Encoding.ASCII.GetString(array, 0, count); if (text2.IndexOf("\r\n\r\n") > -1) { break; } } Stream stream = new FileStream(sFilePath, FileMode.Create); array = new byte[1]; while ((count = socket.Receive(array, array.Length, SocketFlags.None)) > 0) { if (array[0] == 255) { stream.Write(array, 0, count); break; } } array = new byte[1024]; while ((count = socket.Receive(array, array.Length, SocketFlags.None)) > 0) { stream.Write(array, 0, count); } stream.Close(); stream.Dispose(); } socket.Close(); } catch { } } this.SetCookies(text2); result = true; } return(result); }