POST数据到Web服务HttpWebRequest Windows Phone 8

时间:2015-11-13 15:22:02

标签: windows-phone-8

我今天一直在努力尝试将此示例改为POST数据而不是提供的示例GET。

http://blogs.msdn.com/b/andy_wigley/archive/2013/02/07/async-and-await-for-http-networking-on-windows-phone.aspx

我已经取代了这条线:

request.Method = HttpMethod.Get;

 request.Method = HttpMethod.Post;

但是找不到允许我在我希望发布的内容中流式传输的方法。

这个HttpWebRequest似乎比其他方式更清洁,例如发送委托函数来处理响应。

在Wigley先生的示例代码中,我可以看到POST,所以它必须是可能的

 public static class HttpMethod
    {
        public static string Head { get { return "HEAD"; } }
        public static string Post { get { return "POST"; } }

1 个答案:

答案 0 :(得分:0)

我前段时间写过这门课

  public class JsonSend<I, O>
    {
        bool _parseOutput;
        bool _throwExceptionOnFailure;

        public JsonSend()
            : this(true,true)
        {

        }

        public JsonSend(bool parseOutput, bool throwExceptionOnFailure)
        {
            _parseOutput = parseOutput;
            _throwExceptionOnFailure = throwExceptionOnFailure;
        }

        public async Task<O> DoPostRequest(string url, I input)
        {
            var client = new HttpClient();
            CultureInfo ci = new CultureInfo(Windows.System.UserProfile.GlobalizationPreferences.Languages[0]);

            client.DefaultRequestHeaders.Add("Accept-Language", ci.TwoLetterISOLanguageName);
            var uri = new Uri(string.Format(
                url,
                "action",
                "post",
                DateTime.Now.Ticks
                ));

            string serialized = JsonConvert.SerializeObject(input);

            StringContent stringContent = new StringContent(
                serialized,
                Encoding.UTF8,
                "application/json");

            var response = client.PostAsync(uri, stringContent);

            HttpResponseMessage x = await response;
            HttpContent requestContent = x.Content;
            string jsonContent = requestContent.ReadAsStringAsync().Result;
            if (x.IsSuccessStatusCode == false && _throwExceptionOnFailure)
            {
                throw new Exception(url + " with POST ends with status code " + x.StatusCode + " and content " + jsonContent);
            }

            if (_parseOutput == false){
                return default(O);
            }

            return JsonConvert.DeserializeObject<O>(jsonContent);
        }

        public async Task<O> DoPutRequest(string url, I input)
        {
            var client = new HttpClient();
            CultureInfo ci = new CultureInfo(Windows.System.UserProfile.GlobalizationPreferences.Languages[0]);                    

            client.DefaultRequestHeaders.Add("Accept-Language", ci.TwoLetterISOLanguageName);
            var uri = new Uri(string.Format(
                url,
                "action",
                "put",
                DateTime.Now.Ticks
                ));

            string serializedObject = JsonConvert.SerializeObject(input);
            var response = client.PutAsync(uri,
            new StringContent(
                serializedObject,
                Encoding.UTF8,
                "application/json"));

            HttpResponseMessage x = await response;
            HttpContent requestContent = x.Content;
            string jsonContent = requestContent.ReadAsStringAsync().Result;

            if (x.IsSuccessStatusCode == false && _throwExceptionOnFailure)
            {
                throw new Exception(url + " with PUT ends with status code " + x.StatusCode + " and content " + jsonContent);
            }

            if (_parseOutput == false){
                return default(O);
            }

            return JsonConvert.DeserializeObject<O>(jsonContent);
        }
    }

然后,当我想调用它时,我可以使用它如下:

JsonSend<User, RegistrationReceived> register = new JsonSend<User, RegistrationReceived>();
RegistrationReceived responseUser = await register.DoPostRequest("http://myurl", user);