我们知道常用的提交方式有POST和GET,get方式提交的请求有限制,理论上post没有限制,get和post的区别我不在这里说了,我在这里只说怎样拼接以POST方式提交的报文体,在Windows phone 8 中可以利用下面的步骤来写,
string uriStr =http://www.baidu.com; //这里是你要访问的Uri
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uriStr);//创建request对象
request.Method = "POST"; //把提交方式写成post
request.ContentType = "application/x-www-form-urlencoded";
request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), request);
private void GetRequestStreamCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
Stream postStream = request.EndGetRequestStream(asynchronousResult);
string upData="你好,服务器";
byte[] dataArry = Encoding.UTF8.GetBytes(upData);
postStream.Write(dataArry, 0, upData.Length);
postStream.Close();
request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
}
private void GetResponseCallback(IAsyncResult asynchronousResult)//在这个方法里我们可以获得response对象,进行相应的操作
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
}