2015-11-13 63 views
0

今天我一直在嘗試沒有成功,以適應此示例POST數據,而不是提供的示例GET。POST數據到Web服務HttpWebRequest Windows Phone 8

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似乎比其他方式更清潔,例如,發送委託函數來處理響應。

在威格利先生的示例代碼,我可以看到POST所以它必須能夠

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

回答

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