简体   繁体   中英

Http POST in wp8

As I have gone through the examples,I have come across only asynchronous http web request having callback methods,like:-

    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
     }

Kindly Suggest me if we can make a synchronous httpwebrequest for wp8?

Why not try this, it works in a Desktop app:

                using (Stream TextRequestStream = UsedWebRequest.GetRequestStream())
                {
                    TextRequestStream.Write(ByteArray, 0, ByteArray.Length);
                    TextRequestStream.Flush();
                }               
                HttpWebResponse TokenWebResponse = (HttpWebResponse)UsedWebRequest.GetResponse();
                Stream ResponseStream = TokenWebResponse.GetResponseStream();
                StreamReader ResponseStreamReader = new StreamReader(ResponseStream);
                string Response = ResponseStreamReader.ReadToEnd();
                ResponseStreamReader.Close();
                ResponseStream.Close();

Why not try restsharp ?

sample POST code looks like,

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM