这个地址:http://www.56zhongguo.com/lc.asp我的代码:
WebRequests.cs
private HttpWebRequest BuildWebRequest(Uri destination,NetworkCredential credential,string RequestMethod)
{
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(destination);
//webRequest.Accept = "*/*";
//webRequest.AllowAutoRedirect = false;
//webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.0.3705)";
//webRequest.CookieContainer = new CookieContainer();

webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.Method = RequestMethod;
// Set the credentials
if(credential != null)
    webRequest.Credentials = credential;
// Add cookies for this request
//webRequest.CookieContainer.Add(ccContainer.GetCookies(destination));
return webRequest;
} public string PostTo(string Request, Uri destination)
{
byte[] requestBytes = Encoding.ASCII.GetBytes(Request);
// Build the request.
HttpWebRequest webRequest = BuildWebRequest(destination,null,"POST");
webRequest.ContentLength = requestBytes.Length; // Write the request
Stream reqStream = webRequest.GetRequestStream();
reqStream.Write(requestBytes,0,requestBytes.Length);
reqStream.Close();
// Get a response
HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
if (webRequest.HaveResponse)
{
// Read response
StreamReader stream = new StreamReader(webResponse.GetResponseStream(),System.Text.Encoding.GetEncoding("gb2312"));
string responseString = stream.ReadToEnd();
stream.Close();

webResponse.Close();
return responseString;
}
// No response
throw new ApplicationException("No response received from host.");
}//入口点webform1.aspx
private void Button1_Click(object sender, System.EventArgs e)
{
WebRequests myReq = new WebRequests();
string strRequest = "fromareacode=北京&toareacode=广州";

string strResponse = myReq.PostTo(strRequest,new Uri("http://www.56zhongguo.com/lc.asp"));
Response.Write(strResponse);
}
高手帮忙看下,谢谢!

解决方案 »

  1.   

    你的字符带有中文,不能用Ascii去获得byte,需要通过unicode或者其他编码方式
      

  2.   

    change
    byte[] requestBytes = Encoding.ASCII.GetBytes(Request);with
    Encoding encGB = Encoding.GetEncoding( "gb2312" );
    byte[] requestBytes = encGB.GetBytes(Request);
      

  3.   

    我的这种就没有问题。
    public string GetHttp(string sUrl)//取得网页源代码
    {
    WebClient client1 = new WebClient();
    client1.Credentials = CredentialCache.DefaultCredentials;
    byte[] buffer1 = client1.DownloadData(sUrl);
    string text1 = Encoding.Default.GetString(buffer1);
    client1.Dispose();

    return text1;
    }
      

  4.   

    http://www.56zhongguo.com/lc.asp
    这个地址好像有点问题,搞不清楚问题在哪,post别的网站可以,这个不行。
      

  5.   

    你的strRequest中含有中文,所以你用
    byte[] requestBytes = Encoding.ASCII.GetBytes(Request);
    转换肯定是错误的,你试试我前面所说的方法,如果还不行,就看asp端接收什么样的编码。
      

  6.   

    to:Knight94(愚翁)
    我用了Encoding.GetEncoding("GB2312").GetBytes,结果一样的,返回了查询的网页,但是没有应得到的结果。这个页的功能是查询任意两城市间的距离,我没有数据库,所有只好通过这种办法来搜集。 另外:“就看asp端接收什么样的编码”,从哪能看到? http://www.56zhongguo.com/lc.asp这个地址是别人的。再次感谢,兄台可以用我的代码,试一下。用这段代码给别的网页post得到的结果是正确的。
      

  7.   

    http://dotnet.aspx.cc/ShowDetail.aspx?id=ATV1GLXT-65FF-4M82-CT5U-B1J65D3ZN2OK
      

  8.   

    http://dotnet.aspx.cc/ShowDetail.aspx?id=0A6660CE-4138-41EF-B882-15DB65564709
    这2篇文章应该可以解决你的问题
      

  9.   

    就是你参数编码的问题,把
    string strRequest = "fromareacode=北京&toareacode=广州";
    替换成
    string strRequest = "fromareacode=" + HttpUtility.UrlEncode( "北京" )
    + "&toareacode=" + HttpUtility.UrlEncode( "广州" );其他不变,还是用ascii编码即可。
      

  10.   

    我知道问题出在哪了。解决方法:using System.Net;
    using System.Text;
    using System.IO;....
    关键代码:
    Encoding encoding = Encoding.GetEncoding("GB2312");string postData = "fromareacode=" + "北京";
    string strUrl = "http://www.56zhongguo.com/lc.asp";
    postData += ("&toareacode=" + "上海&Submit=查看公里数");
    byte[] data = encoding.GetBytes(postData);// 准备请求...
    HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(strUrl);
    myRequest.Method = "POST";
    myRequest.ContentType="application/x-www-form-urlencoded";
    myRequest.ContentLength = data.Length;
    Stream newStream=myRequest.GetRequestStream();
    // 发送数据
    newStream.Write(data,0,data.Length);
    newStream.Close();WebResponse response = myRequest.GetResponse();Stream resStream = response.GetResponseStream();
    StreamReader sr = new StreamReader(resStream, System.Text.Encoding.Default);
    Response.Write ( sr.ReadToEnd());
    resStream.Close(); 
    sr.Close();关键的地方
    Submit=查看公里数
    这个表单名字没有任何意义,asp却使用它进行判断
      

  11.   

    string postData = "fromareacode=北京&toareacode=上海&Submit=查看公里数";
    Submit=查看公里数不能少