C# wp8中的Http POST
在我阅读这些示例时,我发现只有异步http web请求具有回调方法,如:-C# wp8中的Http POST,c#,windows-phone-8,C#,Windows Phone 8,在我阅读这些示例时,我发现只有异步http web请求具有回调方法,如:- private void getList(string restApiPath, Dictionary<string, string> output, Type type, string label) { webRequest = (HttpWebRequest)WebRequest.Create(restApiPath); path = restApiPat
private void getList(string restApiPath, Dictionary<string, string> output, Type type, string label)
{
webRequest = (HttpWebRequest)WebRequest.Create(restApiPath);
path = restApiPath;
labelValue = label;
webRequest.Method = "POST";
webRequest.ContentType = Globals.POST_CONTENT_TYPE;
webRequest.Headers["st"] = MPFPConstants.serviceType;
webRequest.BeginGetRequestStream(result =>
{
HttpWebRequest request = (HttpWebRequest)result.AsyncState;
// End the stream request operation
Stream postStream = request.EndGetRequestStream(result);
// Create the post data
string reqData = Utils.getStringFromList(output);
string encode = RESTApi.encodeForTokenRequest(reqData);
byte[] byteArray = Encoding.UTF8.GetBytes(encode);
postStream.Write(byteArray, 0, byteArray.Length);
postStream.Close();
request.BeginGetResponse(new AsyncCallback(GetResponseCallbackForSpecificConditions), request);
}, webRequest);
}
private void GetResponseCallbackForSpecificConditions(IAsyncResult ar)
{
//code
}
private void getList(字符串restApiPath、字典输出、类型、字符串标签)
{
webRequest=(HttpWebRequest)webRequest.Create(restApiPath);
path=restApiPath;
标签值=标签;
webRequest.Method=“POST”;
webRequest.ContentType=Globals.POST\u CONTENT\u TYPE;
webRequest.Headers[“st”]=MPFPConstants.serviceType;
webRequest.BeginGetRequestStream(结果=>
{
HttpWebRequest请求=(HttpWebRequest)result.AsyncState;
//结束流请求操作
Stream postStream=request.EndGetRequestStream(结果);
//创建post数据
string reqData=Utils.getStringFromList(输出);
字符串encode=RESTApi.encodeForTokenRequest(reqData);
byte[]byteArray=Encoding.UTF8.GetBytes(encode);
Write(byteArray,0,byteArray.Length);
postStream.Close();
BeginGetResponse(新的AsyncCallback(GetResponseCallbackForSpecificationConditions),request);
},webRequest);
}
私有void GetResponseCallbackForSpecificationConditions(IAsyncResult ar)
{
//代码
}
请建议我是否可以为wp8创建同步httpwebrequest?为什么不试试这个,它可以在桌面应用程序中工作:
使用(Stream TextRequestStream=UsedWebRequest.GetRequestStream())
{
TextRequestStream.Write(ByteArray,0,ByteArray.Length);
TextRequestStream.Flush();
}
HttpWebResponse令牌WebResponse=(HttpWebResponse)UsedWebRequest.GetResponse();
Stream ResponseStream=TokenWebResponse.GetResponseStream();
StreamReader ResponseStreamReader=新的StreamReader(ResponseStream);
字符串响应=ResponseStreamReader.ReadToEnd();
ResponseStreamReader.Close();
ResponseStream.Close();
为什么不试试
示例邮政编码如下所示:
RestClient _authClient = new RestClient("https://sample.com/account/login");
RestRequest _credentials = new RestRequest(Method.POST);
_credentials.AddCookie(_cookie[0].Name, _cookie[0].Value);
_credentials.AddParameter("userLogin", _username, ParameterType.GetOrPost);
_credentials.AddParameter("userPassword", _password, ParameterType.GetOrPost);
_credentials.AddParameter("submit", "", ParameterType.GetOrPost);
RestResponse _credentialResponse = (RestResponse)_authClient.Execute(_credentials);
Console.WriteLine("Authentication phase Uri : " + _credentialResponse.ResponseUri);
没有方法BeginResponse()。因此,webRequest.BeginResponse()不起作用,必须使用webRequest.BeginGetResponse()