【问题标题】:POST data to web service HttpWebRequest Windows Phone 8POST 数据到 Web 服务 HttpWebRequest Windows Phone 8
【发布时间】:2015-11-13 15:22:02
【问题描述】:

我今天一直在尝试使这个示例适应 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"; } }

【问题讨论】:

    标签: windows-phone-8


    【解决方案1】:

    我前段时间写过这门课

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多