【问题标题】:Windowsphone put method to spring restWindowsphone把方法放到弹簧休息
【发布时间】:2014-12-04 21:52:08
【问题描述】:

我是 Windows Phone 新手。我有一个假期对象,我想在我的数据库中更新它。 我使用 Spring 作为 REST API,并且我有一个方法:

@RequestMapping(value="/vacation", method = RequestMethod.PUT, consumes ="application/json")
public void updateVacation(@RequestBody Vacation vacation){
    new VacationDao().update(vacation);
}

在我的 Windows Phone 应用程序中,我有以下 put 方法:

public async void PushSubscription(Subscription subscription)
    {
        try
        {

            var httpWebRequest = (HttpWebRequest) WebRequest.Create(apiUrl);
            httpWebRequest.ContentType = "text/plain; charset=utf-8";
            httpWebRequest.Method = "PUT";

            // Write the request Asynchronously 
            using (var stream = await Task.Factory.FromAsync<Stream>(httpWebRequest.BeginGetRequestStream,
                httpWebRequest.EndGetRequestStream, null))
            {
                //create some json string
                string json = JsonConvert.SerializeObject(subscription);

                // convert json to byte array
                byte[] jsonAsBytes = Encoding.UTF8.GetBytes(json);

                // Write the bytes to the stream
                await stream.WriteAsync(jsonAsBytes, 0, jsonAsBytes.Length);
                MessageBox.Show("Ik geraak hier");
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

我没有任何异常,我确定我在我的 WP 客户端中传递代码,但我无法在服务器上传递代码...

有什么帮助吗?

【问题讨论】:

    标签: c# spring rest windows-phone-8 put


    【解决方案1】:

    是否解决了将 ContentType 设置为 application/json 而不是 text/plain 的问题?

    您也可以尝试使用HttpClient,这比每次都创建 WebRequest 更适合我,而且您编写的代码也更少。

    public async void PushSubscription(Subscription subscription)
    {
        try
        {
            string json = JsonConvert.SerializeObject(subscription);
    
            using (HttpClient client = new HttpClient())
            {
                await client.PutAsync(apiUrl, new StringContent(json, Encoding.UTF8, "application/json"));
            }
    
            MessageBox.Show("Ik geraak hier");
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-28
      • 1970-01-01
      • 2018-07-13
      • 1970-01-01
      • 2016-11-04
      • 2020-03-19
      相关资源
      最近更新 更多