读书人

呼救post方法

发布时间: 2011-12-24 23:03:24 作者: rapoo

求救post方法
我从网上找了一串代码但看不明白
代码如下:post方法,可以处理中文的url

using System;
using System.Net;
using System.IO;
using System.Text;
using System.Web;
using MSXML2;
using System.Threading;

namespace Engine.Activity.FeeAgency
{
public class PostHttp
{
private PostHttp()
{
}

public static string Post_Http(string a_strUrl,string a_strPostData)
{
string strResult = " " ;

try
{
Encoding encoding = Encoding.GetEncoding( "GB2312 ");



string postData = a_strPostData;

string strUrl = a_strUrl;

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();

try
{
HttpWebResponse HttpWResp = (HttpWebResponse)myRequest.GetResponse();

Stream myStream = HttpWResp.GetResponseStream() ;
StreamReader sr = new StreamReader(myStream , Encoding.Default);
StringBuilder strBuilder = new StringBuilder();
while (-1 != sr.Peek())
{
strBuilder.Append(sr.ReadLine());
}

strResult = strBuilder.ToString();
}
catch(Exception exp)
{

strResult = "错误: " + exp.Message ;
}


}
catch(Exception exp)
{

strResult = "错误: " + exp.Message ;

}



return strResult;

}
}
}


其中public static string Post_Http(string a_strUrl,string a_strPostData) 中的a_strPostData指的是什么啊?


[解决办法]
a_strPostData 是你请求的web服务器发送的请求数据,可以用iehttpheaders等工具,查看post或get的数据信息.例如我们点google查询时,通过刚才的工具会看到

POST /urs.asmx?MSPRU-Client-Key=F%2bq5GL96pQo4mGmQr6ex0Q%3d%3d&MSPRU-Patented-Lock=/EIpJV5Jrh4%3d HTTP/1.1
Accept: text/*
SOAPAction: "http://Microsoft.STS.STSWeb/Lookup "
Content-Type: text/xml; charset=utf-8
User-Agent: VCSoapClient
Host: urs.microsoft.com
Content-Length: 628
Cache-Control: no-cache
这样的数据,就是a_strPostData的值

读书人网 >C#

热点推荐