博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Get the source code via a url

Posted on 2008-04-11 17:20  Snapping  阅读(410)  评论(0编辑  收藏  举报
Get the source code via a url

C# code
private string getWebresourceFile1(string url)
{

WebClient myWebClient
= new WebClient();
byte[] myDataBuffer = myWebClient.DownloadData(url); string SourceCode = Encoding.Default.GetString(myDataBuffer);
saveSourceCode(SourceCode);
return SourceCode;
}

方法2
private string getWebresourceFile2(string url)
{
HttpWebRequest request
=(HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response
=(HttpWebResponse)request.GetResponse();
request.Method
= "GET";
Stream receiveStream
=response.GetResponseStream();
StreamReader readStream
=new StreamReader(receiveStream,Encoding.Default);
string SourceCode=readStream.ReadToEnd();
saveSourceCode(SourceCode);
response.Close();
readStream.Close();
return SourceCode;

}
方法3
private string getWebresourceFile3(string url)
{
WebClient wc
= new WebClient();
wc.Credentials
= CredentialCache.DefaultCredentials;
Byte[] pageData
= wc.DownloadData(url);
string SourceCode = Encoding.Default.GetString(pageData);
saveSourceCode(SourceCode);
wc.Dispose();
return SourceCode;
}

方法4
private string getWebresourceFile4(string url)
{
WebClient wc
= new WebClient();
wc.Credentials
= CredentialCache.DefaultCredentials;
Stream resStream
= wc.OpenRead(url);
StreamReader sr
= new StreamReader(resStream,System.Text.Encoding.Default);

string SourceCode = sr.ReadToEnd();
saveSourceCode(SourceCode);
resStream.Close();
wc.Dispose();
return SourceCode;
}
方法5
private string getWebresourceFile5(string url)
{
WebRequest request
= WebRequest.Create(url);
WebResponse response
= request.GetResponse();
Stream resStream
= response.GetResponseStream();
StreamReader sr
= new StreamReader(resStream, System.Text.Encoding.Default);
string SourceCode = sr.ReadToEnd();
saveSourceCode(SourceCode);
resStream.Close();
sr.Close();
return SourceCode;
}