复制代码
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;
    }
复制代码

 

Post提交xml

 

复制代码

代码

复制代码

private string PostXml(string url, string strPost)
{
string result = "";

StreamWriter myWriter 
= null;

HttpWebRequest objRequest 
= (HttpWebRequest)WebRequest.Create(url);
objRequest.Method 
= "POST";
objRequest.ContentLength 
= strPost.Length;
objRequest.ContentType 
= "text/xml";//提交xml 
//objRequest.ContentType = "application/x-www-form-urlencoded";//提交表单
try
{
myWriter 
= new StreamWriter(objRequest.GetRequestStream());
myWriter.Write(strPost);
}
catch (Exception e)
{
return e.Message;
}
finally
{
myWriter.Close();
}

HttpWebResponse objResponse 
= (HttpWebResponse)objRequest.GetResponse();
using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()))
{
result 
= sr.ReadToEnd();
sr.Close();
}
return result;
}