我想程序通过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),"")
这个却不行,取到的网站内容不是“上海”的信息,而是“北京”的信息,我发现如果没有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),"")
{
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
}
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,"");
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);
这样就可以
不可以的话 请你吃饭。哈哈
int start=weacherhtml.IndexOf("时至");
int end=weacherhtml.IndexOf("image/left.gif");
return weacherhtml.Substring(start,end-start+2);
你可以在你的html中找特定的最靠近你要的信息的地方找一些这种关键词.然后截取大概,然后在做详细的处理
你还真得请我吃饭!
你自己试试,看看行不行,
我说了GB2312我试过了不行。
你这方法,不用试,和我的效果一样,一般网站可以,这个不行,你自己试试就知道了
你们的方法没问题,是我太不仔细了请你们到下面的帖子接分
http://community.csdn.net/Expert/topic/4792/4792645.xml?temp=.8794824
http://community.csdn.net/Expert/topic/4792/4792029.xml?temp=5.138797E-02
再次表示歉意和感谢
2.在受值处理页面做判断,如无参数,则做其他处理,或转向统一信息处理也面.
3.try 在webconfig文件内把encoding 改为GB2312.