C# post提交表单的例程

protected string GetWebContent(string url)
    {
        Stream outstream 
= null;
        Stream instream 
= null;
        StreamReader sr 
= null;
        HttpWebResponse response 
= null;
        HttpWebRequest request 
= null;
        
// 要注意的这是这个编码方式,还有内容的Xml内容的编码方式
        Encoding encoding = Encoding.GetEncoding("GBK");
        
byte[] data = encoding.GetBytes(url);

        
// 准备请求,设置参数
        request = WebRequest.Create(url) as HttpWebRequest;
        request.Method 
= "POST";
        
//request.ContentType = "text/plain";
        request.ContentLength = data.Length;

        outstream 
= request.GetRequestStream();
        outstream.Write(data, 
0, data.Length);
        outstream.Flush();
        outstream.Close();
        
//发送请求并获取相应回应数据

        response 
= request.GetResponse() as HttpWebResponse;
        
//直到request.GetResponse()程序才开始向目标网页发送Post请求
        instream = response.GetResponseStream();
        sr 
= new StreamReader(instream, encoding);
        
//返回结果网页(html)代码

        
string content = sr.ReadToEnd();
        
return content;
    }

posted @ 2010-02-15 12:16  猪悟能  阅读(555)  评论(0编辑  收藏  举报