我想程序通过url地址取得网站内容,大部分网站都可以,可是http://www.cma.gov.cn/cma_new/tqyb/gn_city.php?city=上海
这个却不行,取到的网站内容不是“上海”的信息,而是“北京”的信息,我发现如果没有city这个参数默认的就是北京,所以可能问题出在这个参数上。
我知道传中文参数要处理,可是试了GB2312、Default、UTF8、Unicode都不行,实在没办法了,大家帮忙啊,谢谢了
代码如下:
private static string getPage(String url,String payload)
{
StringBuilder strRet = new StringBuilder();
WebResponse result = null;
try
{
WebRequest req = WebRequest.Create(url);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
StringBuilder UrlEncoded = new StringBuilder();
Char[] reserved = {'?', '=', '&'};
byte[] SomeBytes = null;if (payload != null)
{
int i=0, j;
while(i<payload.Length)
{
j=payload.IndexOfAny(reserved, i);
if (j==-1)
{
UrlEncoded.Append(payload.Substring(i, payload.Length-i));
break;
}
UrlEncoded.Append(payload.Substring(i, j-i));
UrlEncoded.Append(payload.Substring(j,1));
i = j+1;
}
SomeBytes = Encoding.Default.GetBytes(UrlEncoded.ToString());
req.ContentLength = SomeBytes.Length;
Stream newStream = req.GetRequestStream();
newStream.Write(SomeBytes, 0, SomeBytes.Length);
newStream.Close();
}
else
{
req.ContentLength = 0;
}
result = req.GetResponse();
Stream ReceiveStream = result.GetResponseStream();
Encoding encode = System.Text.Encoding.Default;
StreamReader sr = new StreamReader( ReceiveStream, encode );
Char[] read = new Char[256];
int count = sr.Read( read, 0, 256 );
while (count > 0)
{
String str = new String(read, 0, count);
strRet.Append(str);
count = sr.Read(read, 0, 256);
}

catch
{

finally
{
if ( result != null )
{
result.Close();
}
}
return strRet.ToString();
}
这是我的调用代码:getPage("http://www.cma.gov.cn/cma_new/tqyb/gn_city.php","city="+System.Web.HttpUtility.UrlEncode("上海",Encoding.Default))
getPage("http://www.cma.gov.cn/cma_new/tqyb/gn_city.php?city="+System.Web.HttpUtility.UrlEncode("上海",Encoding.Default),"")

解决方案 »

  1.   

    http://www.cma.gov.cn/cma_new/tqyb/gn_city.php?city=上海是上海啊!??
      

  2.   

    private string gethtmlContent(string url)
    {
    string weacherhtml=String.Empty;
    HttpWebRequest rt=null;
    HttpWebResponse rs=null;
    Stream stream=null;
    StreamReader sr=null;
     
    try
    {
    rt=(HttpWebRequest)WebRequest.Create(url);
    rs=(HttpWebResponse)rt.GetResponse();
    stream=rs.GetResponseStream();
    sr=new StreamReader(stream,System.Text.Encoding.Default);
    weacherhtml=sr.ReadToEnd();
       
    }
    catch(Exception ee)
    {
    weacherhtml="获取数据出现错误";
    }
    finally
    {
    sr.Close();
    stream.Close();
    rs.Close();
    }
          return weacherhtml
    }
      

  3.   

    用以上代码来获取html应该可以。我一直这样用的。没出现问题
      

  4.   

    这样OK
    string sphr=System.Web.HttpUtility.UrlEncode("上海",System.Text.Encoding.GetEncoding("gb2312"));
    label1.Text = sphr;
    textBox2.Text = getPage("http://www.cma.gov.cn/cma_new/tqyb/gn_city.php?city="+sphr+"&province="+sphr,"");
      

  5.   

    用xiaomatian(趴趴熊◎%#……※×) 的方法加上
    string sphr=System.Web.HttpUtility.UrlEncode(textBox1.Text,System.Text.Encoding.GetEncoding("gb2312"));
    label1.Text = sphr;
    textBox2.Text = gethtmlContent("http://www.cma.gov.cn/cma_new/tqyb/gn_city.php?city="+sphr);
    这样就可以
    不可以的话 请你吃饭。哈哈
      

  6.   

    顺便请教一下!就是得到了她的html代码,通过什么方法来获取需要的东西呢,就是天气情况!
      

  7.   

    http://community.csdn.net/Expert/topic/4794/4794120.xml?temp=.5041773
      

  8.   

    呵呵要获取指定的内容你可以以它的一个特定的样式来大致的截取,如
    int start=weacherhtml.IndexOf("时至");
    int end=weacherhtml.IndexOf("image/left.gif");
    return weacherhtml.Substring(start,end-start+2);
    你可以在你的html中找特定的最靠近你要的信息的地方找一些这种关键词.然后截取大概,然后在做详细的处理
      

  9.   

    soarheaven() 
    你还真得请我吃饭!
    你自己试试,看看行不行,
    我说了GB2312我试过了不行。
      

  10.   

    xiaomatian(趴趴熊◎%#……※×) 
    你这方法,不用试,和我的效果一样,一般网站可以,这个不行,你自己试试就知道了
      

  11.   

    我再说一便取到的网站内容不是“上海”的信息,而是“北京”的信息,我发现如果没有city这个参数默认的就是北京,所以可能问题出在这个参数上,可是试了GB2312、Default、UTF8、Unicode都不行
      

  12.   

    你写一个HTML页面,存成不同的格式,去掉IE里的“总是用UTF8发送URL”然后试一下哪一个能够成功。
      

  13.   

    现在正式向xiaomatian(趴趴熊◎%#……※×) 和soarheaven() 道歉
    你们的方法没问题,是我太不仔细了请你们到下面的帖子接分
    http://community.csdn.net/Expert/topic/4792/4792645.xml?temp=.8794824
    http://community.csdn.net/Expert/topic/4792/4792029.xml?temp=5.138797E-02
    再次表示歉意和感谢
      

  14.   

    以下纯属个人意见,请误BT 1.最好别用直接的 值 来,进行页面的传递工作.
    2.在受值处理页面做判断,如无参数,则做其他处理,或转向统一信息处理也面.
    3.try 在webconfig文件内把encoding 改为GB2312.