C#请求远程服务器的资源
using System.IO;
1、获取远程服务器文件的大小
public static long GetRemoteHTTPFileSize(string sURL)
{
long size = 0L;
try
{
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(sURL);
request.Method = "HEAD";
System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
size = response.ContentLength;
response.Close();
}
catch
{
size = 0L;
}
return size;
}
2、下载远程服务器的文件
public static void GetRemoteHTTPFileDown(string sURL)
{
Stream content = null;
try
{
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(URL);
request.Method = "GET";
System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
content = response.GetResponseStream();
StreamReader sr = new StreamReader(content, System.Text.Encoding.Default);
string str = sr.ReadToEnd();
StreamWriter sw = new StreamWriter(Server.MapPath("~/1.txt"), false, System.Text.Encoding.Unicode);
sw.Write(str);
sw.Flush();
sw.Close();
sr.Close();
response.Close();
}
catch
{ }
}
lei1217