如何使用参数发送POST请求的异步的Windows Phone 8 [英] How to send POST request with parameters asynchronously in windows phone 8

查看:136
本文介绍了如何使用参数发送POST请求的异步的Windows Phone 8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想发送POST请求的Windows Phone 8的环境我的code运行成功,但我得到NOTFOUND例外。它的意思是我想发布一些数据,但我发送空。所以,请让我知道如何与数据异步发送POST请求中的Windows Phone 8 environmet。我想下面的链接,但没有帮助。
<一href=\"http://stackoverflow.com/questions/18816990/webrequest-has-no-getresponse-method-windows-phone-8\">link <一href=\"http://stackoverflow.com/questions/16693425/windows-phone-8-post-json-in-request-body-give-an-exception\">link2

I would like to send POST request in windows phone 8 environment my code is running successfully but i am getting NotFound exception. Its mean is i want to POST some data but i am sending null. So please let me know how to send POST Request asynchronously with Data in windows phone 8 environmet. I tried following links but not helpful. link link2

我走近这样

private async Task<LastRead> SyncLastReadPOST(LastRead lastreads, bool actionStatus)
{
    string jsondata = "";
    actionStatus = false;
    apiData = new LastReadAPI()//It is global variable from apiData this object has the information
    {
        AccessToken = thisApp.currentUser.AccessToken,
        Book = lastreads.Book,
        Page = lastreads.Page,
        Device = lastreads.Device
    };
    jsondata = Newtonsoft.Json.JsonConvert.SerializeObject(apiData);
    LastRead responsedData = new LastRead();
    Uri lastread_url = new Uri(string.Format("{0}lastread", url_rootPath));
    try
    {
        HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(lastread_url);
        webRequest.ContentType = "application/json";
        webRequest.Accept = "application/json;odata=verbose";
        webRequest.Method = "POST";
        webRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), webRequest);
    }
    catch { }
    return responsedData;
}

private void GetRequestStreamCallback(IAsyncResult ar)
{
    HttpWebRequest request = (HttpWebRequest)ar.AsyncState;
    Stream postStream = request.EndGetRequestStream(ar);
    var input = Newtonsoft.Json.JsonConvert.SerializeObject(jsondata);//jsondata is my global data variable in json format.
    byte[] byteArray = Encoding.UTF8.GetBytes(input);
    postStream.WriteAsync(byteArray, 0, byteArray.Length);
    postStream.Close();
    request.BeginGetResponse(new AsyncCallback(GetResponseStreamCallback), request);
}

private void GetResponseStreamCallback(IAsyncResult ar)
{
    try
    {
        HttpWebRequest webRequest = (HttpWebRequest)ar.AsyncState;
        HttpWebResponse response;
        //In following line i am getting the exception notFound.
        response = (HttpWebResponse)webRequest.EndGetResponse(ar);
        Stream streamResponse = response.GetResponseStream();
        StreamReader streamReaders = new StreamReader(streamResponse);
        var responces = streamReaders.ReadToEnd();
        streamResponse.Close();
        streamReaders.Close();
        response.Close();
    }
    catch(Exception ex)
    {
    }
}

当我们在使用POST请求方法不张贴任何数据,因为据我所知NOTFOUND异常来。你可以看到我已经提到我传递到GEtRequestStreamCallback数据。我刚才提到的说明。请帮帮我。我要去的地方错误的。

As far as i know notFound exceptions comes when we are not posting any data while using the POST request method. you can see i have mentioned the data i am passing into the GEtRequestStreamCallback. I have mentioned a note. Please help me. Where i am going to wrong.

推荐答案

我与Web客户端的就地的HttpClient的帮助下做到了。下面的几行会做魔术。 :)

I did it with the help of HttpClient inplace of WebClient. Following few lines will do magic. :)

HttpClient hc = new HttpClient();
hc.BaseAddress = new Uri(annotation_url.ToString());
HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Post, myUrl);
HttpContent myContent = req.Content = new StringContent(myJsonString, Encoding.UTF8, "application/json");
var response = await hc.PostAsync(myUrl, myContent);

//Line for pull out the value of content key value which has the actual resposne.
string resutlContetnt = response.Content.ReadAsStringAsync().Result;
DataContractJsonSerializer deserializer_Json = new DataContractJsonSerializer(typeof(MyWrapperClass));
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(resutlContetnt.ToString()));
AnnotateResponse = deserializer_Json.ReadObject(ms) as MyWrapperClass;

这篇关于如何使用参数发送POST请求的异步的Windows Phone 8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆