string filename=Server.MapPath("0124-1.html");
System.IO.StreamReader ret=new StreamReader (filename,System.Text.Encoding.GetEncoding("GB2312"));
Response.Write (System.Web.HttpUtility.HtmlEncode(ret.ReadToEnd()));
sr.Close();这是我的代码,但读取的是本地才行若替换成
string filename=Server.MapPath("http://www.ebigchina.com/ebcps/2/pl/0124-1.html");则提示:MapPath“http://www.ebigchina.com/ebcps/2/pl/0124-1.html”的路径无效。应为虚拟路径
各位大虾,要如何解决?
System.IO.StreamReader ret=new StreamReader (filename,System.Text.Encoding.GetEncoding("GB2312"));
Response.Write (System.Web.HttpUtility.HtmlEncode(ret.ReadToEnd()));
sr.Close();这是我的代码,但读取的是本地才行若替换成
string filename=Server.MapPath("http://www.ebigchina.com/ebcps/2/pl/0124-1.html");则提示:MapPath“http://www.ebigchina.com/ebcps/2/pl/0124-1.html”的路径无效。应为虚拟路径
各位大虾,要如何解决?
System.IO.StreamReader ret=new System.IO.StreamReader (filename,System.Text.Encoding.GetEncoding("GB2312"));
Response.Write (System.Web.HttpUtility.HtmlEncode(ret.ReadToEnd()));
ret.Close();
出现的提示是"不支持 URI 格式。"
{
string strResult;
try
{
HttpWebRequest myReq = (HttpWebRequest)HttpWebRequest.Create(requestUrl) ;
myReq.Timeout = timeout;
HttpWebResponse HttpWResp = (HttpWebResponse)myReq.GetResponse();
Stream myStream = HttpWResp.GetResponseStream () ;
StreamReader sr = new StreamReader(myStream , Encoding.GetEncoding("UTF-8"));
StringBuilder strBuilder = new StringBuilder(); while (-1 != sr.Peek())
{
strBuilder.Append(sr.ReadLine()+"\r\n");
}
strResult = strBuilder.ToString();
}
catch(Exception exp)
{
strResult = "请求错误:" + exp.Message;
}
return strResult ;
}
WebClient wc = new WebClient();
wc.Credentials = CredentialCache.DefaultCredentials;
Byte[] pageData = wc.DownloadData(PageUrl);方法二:WebRequest
WebRequest request = WebRequest.Create(PageUrl);
WebResponse response = request.GetResponse();
Stream resStream = response.GetResponseStream();
string URlLAddress="http://www.163.com";
WebRequest myre=WebRequest.Create(URlLAddress); string tempPath=Application.StartupPath +"\\temp";
if(!Directory.Exists(tempPath))
Directory.CreateDirectory(tempPath);
string tempName=tempPath+"\\tmp.html";
WebClient client=new WebClient();
client.DownloadFile(URlLAddress,tempName); Stream str=client.OpenRead(URlLAddress);
byte[] mbyte=new byte[10000];
int allmbyte=(int)mbyte.Length;
int startmbyte=0;
//接收数据
while(allmbyte>0)
{
int m=str.Read(mbyte,startmbyte,allmbyte);
if(m==0)
break;
startmbyte+=m;
allmbyte-=m;
Application.DoEvents();
}
//写入临时数据
FileStream fstr=new FileStream(tempName,FileMode.OpenOrCreate,FileAccess.Write);
fstr.Write(mbyte,0,startmbyte);
fstr.Close(); //////读取文件 StreamReader sr=new StreamReader(tempName,Encoding.Default);
try
{
//读取文件
string s;
bool boolread=true;
while(boolread==true)
{
s=sr.ReadLine();
if(s==null)
break;
if(s!="")
{
textBox2.Text+=s;
}
Application.DoEvents();
}
}
catch(Exception ex)
{
MessageBox.Show("发生错误\r\n"+ex.Message);
}
sr.Close();
MessageBox.Show("OK");