HttpWebRequest post提交取得返回值

using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.IO;
using System.Text;

namespace hero3
{
    
public partial class searchmob : System.Web.UI.Page
    {
        
protected void Page_Load(object sender, EventArgs e)
        {
            ASCIIEncoding encoding 
= new ASCIIEncoding();
            
string postData = "mobile=" + Request.QueryString["mob"+ "&action=mobile";
            
byte[] data = encoding.GetBytes(postData);

            
// Prepare web request
            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://www.xxx.com/xxx.aspx");

            myRequest.Method 
= "POST";
            myRequest.ContentType 
= "application/x-www-form-urlencoded";
            myRequest.ContentLength 
= data.Length;
            Stream newStream 
= myRequest.GetRequestStream();

            
// Send the data.
            newStream.Write(data, 0, data.Length);
            newStream.Close();

            
// Get response
            HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
            StreamReader reader 
= new StreamReader(myResponse.GetResponseStream(), Encoding.Default);
            
string content = reader.ReadToEnd();
            
string con = content.Substring(content.IndexOf("xxx"+ 522);
            Response.Write(con.Trim()); 
        }
    }
}
posted @ 2009-10-29 09:35  asp.net初学者  阅读(1474)  评论(0编辑  收藏  举报