如何使用Socket来模拟HTTP请求与相应 用 Fillder2 抓一下浏览器的包,然后再看看自己发的 http request,就知道错在什么地方了。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你用fiddler看看ie发的数据,有很多header要发送的 *****************************************************************************签名档: http://feiyun0112.cnblogs.com/ 我完全没搞懂你要实现什么...虽然说http最下面 也是tcp/ip.. 网站虽然也是80端口但是那是http啊 你不能直接拿tcp/ip直接连吧.. 看到是同簇的就给你我05年就用的Socket写的HttpRequest public class MyWebRequest { public MyWebRequest(Uri uri) { this._RequestUri=uri; this._Referer=""; this._UserAgent="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322)"; this._Cookie=""; this._AcceptType="*/*"; this._Method = "GET"; this._Timeout=15; } private void IniHeader() { this._Headers["Host"] = _RequestUri.Host; if(this._Referer!="") this._Headers["Referer"] = this.Referer; if(this._UserAgent!="") _Headers["User-Agent"] = this.UserAgent; if(this._Cookie!="") this._Headers["Cookie"] =this._Cookie; this._Headers["Connection"] = "Keep-Alive"; } public static MyWebRequest Create(string Url) { Uri uri=new Uri(Url); return Create(uri); } public static MyWebRequest Create(Uri uri) { return new MyWebRequest(uri); } public MyWebResponse GetResponse() { this.IniHeader(); if(this._response == null || this._response.Socket == null || this._response.Socket.Connected == false) { this._response = new MyWebResponse(); this._response.Connect(this); this._response.SetTimeout(this._Timeout); } this._response.SendRequest(this); this._response.ReceiveHeader(); return this._response; } private string _Header; private Uri _RequestUri; private string _Method; private MyWebResponse _response; private WebHeaderCollection _Headers = new WebHeaderCollection(); private string _Referer=""; private string _UserAgent=""; private string _Cookie=""; private string _AcceptType; private int _Timeout; #region Response用的 public string Header { get{return this._Header;} set{this._Header=value;} } public Uri RequestUri { get{return this._RequestUri;} set{this._RequestUri=value;} } public string Method { get{return this._Method;} set{this._Method=value;} } public WebHeaderCollection Headers { get{return this._Headers;} set{this._Headers=value;} } #endregion public int Timeout { set{this._Timeout=value;} get{return this._Timeout;} } public string Accept { set{this._AcceptType=value;} get{return this._AcceptType;} } public string Referer { set{this._Referer=value;} get{return this._Referer;} } public string Cookie { set{this._Cookie=value;} get{return this._Cookie;} } public string UserAgent { set{this._UserAgent=value;} get{return this._UserAgent;} } }//这个是public class MyWebResponse { public MyWebResponse() { } public void Connect(MyWebRequest request) { ResponseUri = request.RequestUri; Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPEndPoint remoteEP = new IPEndPoint(Dns.Resolve(ResponseUri.Host).AddressList[0], ResponseUri.Port); Socket.Connect(remoteEP); } public void SendRequest(MyWebRequest request) { ResponseUri = request.RequestUri; request.Header = request.Method+" "+ResponseUri.PathAndQuery+" HTTP/1.0\r\n"+request.Headers; Socket.Send(Encoding.ASCII.GetBytes(request.Header)); } public void SetTimeout(int Timeout) { Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, Timeout*1000); Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, Timeout*1000); } public void ReceiveHeader() { this._Header = ""; this._Headers = new WebHeaderCollection(); byte[] bytes = new byte[10]; while(Socket.Receive(bytes, 0, 1, SocketFlags.None) > 0) { this._Header += Encoding.Default.GetString(bytes, 0, 1); if(bytes[0] == '\n' && this._Header.EndsWith("\r\n\r\n")) break; } MatchCollection matches = new Regex("[^\r\n]+").Matches(this._Header.TrimEnd('\r', '\n')); for(int n = 1; n < matches.Count; n++) { string[] strItem = matches[n].Value.Split(new char[] { ':' }, 2); if(strItem.Length > 0) this._Headers[strItem[0].Trim()] = strItem[1].Trim(); } // check if the page should be transfered to another location if( matches.Count > 0 && ( matches[0].Value.IndexOf(" 302 ") != -1 || matches[0].Value.IndexOf(" 301 ") != -1)) // check if the new location is sent in the "location" header if(this._Headers["Location"] != null) { try { ResponseUri = new Uri(this._Headers["Location"]); this._Location=this._Headers["Location"]; } catch { this.ResponseUri = new Uri(this.ResponseUri,this._Headers["Location"]); } } this._ContentType = this._Headers["Content-Type"]; if(this._Headers["Content-Length"] != null) { if(int.Parse(this._Headers["Content-Length"])==0) this._ContentLength = -1; else this._ContentLength =int.Parse(this._Headers["Content-Length"]); } this._KeepAlive = (Headers["Connection"] != null && Headers["Connection"].ToLower() == "keep-alive") || (Headers["Proxy-Connection"] != null && Headers["Proxy-Connection"].ToLower() == "keep-alive"); } public void Close() { Socket.Close(); } private Uri ResponseUri; private string _Header; private WebHeaderCollection _Headers; private Socket _Socket; private string _Location; private string _ContentType; private long _ContentLength=-1; private bool _KeepAlive; public string Header { get{return _Header;} set{_Header=value;} } public WebHeaderCollection Headers { get{return this._Headers;} set{this._Headers=value;} } public Socket Socket { get{return this._Socket;} set{this._Socket=value;} } public string Location { get{return this._Location;} set{this._Location=value;} } public string ContentType { get{return this._ContentType;} set{this._ContentType=value;} } public long ContentLength { get{return this._ContentLength;} set{this._ContentLength=value;} } public bool KeepAlive { get{return this._KeepAlive;} set{this._KeepAlive=value;} } }调用例子MyWebRequest request = MyWebRequest.Create(Url); request.Referer = Referer; request.Accept = "*/*"; request.UserAgent = this._UserAgent; request.Cookie=Cookie; request.Timeout = Timeout * 1000; MyWebResponse response = request.GetResponse(); while ((nBytes = response.Socket.Receive(RecvBuffer, 0, byteLen, SocketFlags.None)) > 0) { nTotalBytes += nBytes; strResponse += encoding.GetString(RecvBuffer, 0, nBytes); int nProgressPercentage=(int)(100 - (response.ContentLength - nTotalBytes) * 100 / response.ContentLength); this.OnDownloadStringProgressChanged(new DownloadProgressChangedEventArgs(nProgressPercentage,nTotalBytes,response.ContentLength)); Thread.Sleep(10); if (response.KeepAlive && nTotalBytes >= response.ContentLength && response.ContentLength > 0) break; } if(response.ContentLength==-1) { this.OnDownloadStringProgressChanged(new DownloadProgressChangedEventArgs(100,nTotalBytes,nTotalBytes)); } winform接收http请求数据 请问在vs2010里,源代码管理资源管理器是什么来的?它的作用是什么? asp.net【多文件上传,并保存到数据库】 Activator.CreateInstance如何传入函数指针 C#.NET调用Matlab 用OLEDB读取excel数据,如果读取第一张表 更新语句中取值 dropdownlist控件的选择怎么才能有效?大家帮帮我吧! 求助String与string有什么区别 关于C#遍译可执行程序问题 wpf combobox内嵌treeview的问题 求教XML序列化特殊格式
*****************************************************************************
签名档: http://feiyun0112.cnblogs.com/
{
public MyWebRequest(Uri uri)
{
this._RequestUri=uri;
this._Referer="";
this._UserAgent="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322)";
this._Cookie="";
this._AcceptType="*/*";
this._Method = "GET";
this._Timeout=15;
}
private void IniHeader()
{
this._Headers["Host"] = _RequestUri.Host; if(this._Referer!="")
this._Headers["Referer"] = this.Referer; if(this._UserAgent!="")
_Headers["User-Agent"] = this.UserAgent; if(this._Cookie!="")
this._Headers["Cookie"] =this._Cookie; this._Headers["Connection"] = "Keep-Alive";
}
public static MyWebRequest Create(string Url)
{
Uri uri=new Uri(Url);
return Create(uri);
}
public static MyWebRequest Create(Uri uri)
{
return new MyWebRequest(uri);
}
public MyWebResponse GetResponse()
{
this.IniHeader();
if(this._response == null || this._response.Socket == null || this._response.Socket.Connected == false)
{
this._response = new MyWebResponse();
this._response.Connect(this);
this._response.SetTimeout(this._Timeout);
}
this._response.SendRequest(this);
this._response.ReceiveHeader();
return this._response;
}
private string _Header;
private Uri _RequestUri;
private string _Method;
private MyWebResponse _response;
private WebHeaderCollection _Headers = new WebHeaderCollection();
private string _Referer="";
private string _UserAgent="";
private string _Cookie="";
private string _AcceptType;
private int _Timeout;
#region Response用的 public string Header
{
get{return this._Header;}
set{this._Header=value;}
} public Uri RequestUri
{
get{return this._RequestUri;}
set{this._RequestUri=value;}
}
public string Method
{
get{return this._Method;}
set{this._Method=value;}
} public WebHeaderCollection Headers
{
get{return this._Headers;}
set{this._Headers=value;}
}
#endregion
public int Timeout
{
set{this._Timeout=value;}
get{return this._Timeout;}
}
public string Accept
{
set{this._AcceptType=value;}
get{return this._AcceptType;}
} public string Referer
{
set{this._Referer=value;}
get{return this._Referer;}
}
public string Cookie
{
set{this._Cookie=value;}
get{return this._Cookie;}
}
public string UserAgent
{
set{this._UserAgent=value;}
get{return this._UserAgent;}
}
}
//这个是
public class MyWebResponse
{
public MyWebResponse()
{
}
public void Connect(MyWebRequest request)
{
ResponseUri = request.RequestUri;
Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint remoteEP = new IPEndPoint(Dns.Resolve(ResponseUri.Host).AddressList[0], ResponseUri.Port);
Socket.Connect(remoteEP);
}
public void SendRequest(MyWebRequest request)
{
ResponseUri = request.RequestUri; request.Header = request.Method+" "+ResponseUri.PathAndQuery+" HTTP/1.0\r\n"+request.Headers;
Socket.Send(Encoding.ASCII.GetBytes(request.Header));
}
public void SetTimeout(int Timeout)
{
Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, Timeout*1000);
Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, Timeout*1000);
}
public void ReceiveHeader()
{
this._Header = "";
this._Headers = new WebHeaderCollection(); byte[] bytes = new byte[10];
while(Socket.Receive(bytes, 0, 1, SocketFlags.None) > 0)
{
this._Header += Encoding.Default.GetString(bytes, 0, 1);
if(bytes[0] == '\n' && this._Header.EndsWith("\r\n\r\n"))
break;
}
MatchCollection matches = new Regex("[^\r\n]+").Matches(this._Header.TrimEnd('\r', '\n'));
for(int n = 1; n < matches.Count; n++)
{
string[] strItem = matches[n].Value.Split(new char[] { ':' }, 2);
if(strItem.Length > 0)
this._Headers[strItem[0].Trim()] = strItem[1].Trim();
}
// check if the page should be transfered to another location
if( matches.Count > 0 && (
matches[0].Value.IndexOf(" 302 ") != -1 ||
matches[0].Value.IndexOf(" 301 ") != -1))
// check if the new location is sent in the "location" header
if(this._Headers["Location"] != null)
{
try
{ ResponseUri = new Uri(this._Headers["Location"]);
this._Location=this._Headers["Location"];
}
catch
{ this.ResponseUri = new Uri(this.ResponseUri,this._Headers["Location"]);
}
}
this._ContentType = this._Headers["Content-Type"];
if(this._Headers["Content-Length"] != null)
{
if(int.Parse(this._Headers["Content-Length"])==0)
this._ContentLength = -1;
else
this._ContentLength =int.Parse(this._Headers["Content-Length"]);
} this._KeepAlive = (Headers["Connection"] != null && Headers["Connection"].ToLower() == "keep-alive") ||
(Headers["Proxy-Connection"] != null && Headers["Proxy-Connection"].ToLower() == "keep-alive");
}
public void Close()
{
Socket.Close();
}
private Uri ResponseUri;
private string _Header;
private WebHeaderCollection _Headers;
private Socket _Socket;
private string _Location;
private string _ContentType;
private long _ContentLength=-1;
private bool _KeepAlive;
public string Header
{
get{return _Header;}
set{_Header=value;}
}
public WebHeaderCollection Headers
{
get{return this._Headers;}
set{this._Headers=value;}
} public Socket Socket
{
get{return this._Socket;}
set{this._Socket=value;}
}
public string Location
{
get{return this._Location;}
set{this._Location=value;}
} public string ContentType
{
get{return this._ContentType;}
set{this._ContentType=value;}
} public long ContentLength
{
get{return this._ContentLength;}
set{this._ContentLength=value;}
}
public bool KeepAlive
{
get{return this._KeepAlive;}
set{this._KeepAlive=value;}
}
}
调用例子
MyWebRequest request = MyWebRequest.Create(Url);
request.Referer = Referer;
request.Accept = "*/*";
request.UserAgent = this._UserAgent;
request.Cookie=Cookie;
request.Timeout = Timeout * 1000;
MyWebResponse response = request.GetResponse();
while ((nBytes = response.Socket.Receive(RecvBuffer, 0, byteLen, SocketFlags.None)) > 0)
{
nTotalBytes += nBytes;
strResponse += encoding.GetString(RecvBuffer, 0, nBytes); int nProgressPercentage=(int)(100 - (response.ContentLength - nTotalBytes) * 100 / response.ContentLength);
this.OnDownloadStringProgressChanged(new DownloadProgressChangedEventArgs(nProgressPercentage,nTotalBytes,response.ContentLength));
Thread.Sleep(10); if (response.KeepAlive && nTotalBytes >= response.ContentLength && response.ContentLength > 0)
break;
}
if(response.ContentLength==-1)
{
this.OnDownloadStringProgressChanged(new DownloadProgressChangedEventArgs(100,nTotalBytes,nTotalBytes));
}