1. 程式人生 > >socket模擬http請求

socket模擬http請求



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));
}