中文问题
问题
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream(),Encoding.Default);
string result = sr.ReadToEnd();
sr.Close();
string b = "测试 ";
为什么 result.IndexOf(b) 会找不到呢
result 里是肯定有测试这两个字的
用的是英文的 XP VS2005
如果我把网页内容存入TXT文件
然后读取TXT内容
result.IndexOf(b) 就可以找到了
------解决方案--------------------using System;
using System.Net;
using System.Text;
using System.IO;
using System.Windows.Forms;
namespace NameParser
{
/// <summary>
/// GetHttpSource 的摘要说明。
/// </summary>
public class GetHttpSource
{
public GetHttpSource()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public static string GetHttpSourceValue(string a_strUrl)
{
string strResult;
HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(new System.Uri(a_strUrl));
myReq.Method = "GET ";
myReq.Accept = "*/* ";
myReq.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322) ";
try
{
HttpWebResponse HttpWResp = (HttpWebResponse)myReq.GetResponse();
string tmp = myReq.Headers.ToString();
Stream myStream = HttpWResp.GetResponseStream();
StreamReader sr = new StreamReader(myStream, Encoding.Default);
StringBuilder strBuilder = new StringBuilder();
while (-1 != sr.Peek())
{
strBuilder.Append(sr.ReadLine() + "\r\n ");
}
strResult = strBuilder.ToString();
// StreamWriter sw = new StreamWriter( "E:\\1.txt ", false, Encoding.Default);
// sw.Write(strResult);
myStream.Close();
sr.Close();
// sw.Close();
}
catch (Exception exp)
{
strResult = "错误: " + exp.Message;
}
return strResult;
StreamWriter sw = new StreamWriter(SaveFileName(), false, Encoding.Default);
sw.Write(body);
sw.Close();
}
}
}
楼主用这个吧..
------解决方案--------------------对,要转编码的。
------解决方案--------------------明显编码问题了
Encoding.Default
这个