使用 WP7 中的 webReuest 通过 Rest APi POST json

发布于 2024-11-06 13:14:15 字数 2034 浏览 0 评论 0原文

我正在使用 Rest API 更新force.com 上的数据库表。我正在发布 json 数据来更新数据库表,如下所示。

     // preparing webrequest
      HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);

      // adding request headers
      request.ContentType = "application/json";
      request.Headers["Authorization"] = "OAuth " + token;
      request.Headers["X-PrettyPrint"] = "1";
      // request method
      request.Method = "PATCH";
       // start the asynchronous operation
                request.BeginGetRequestStream(new AsyncCallback(SaveBeginGetRequestStreamCallBack), request);


 private void SaveBeginGetRequestStreamCallBack(IAsyncResult ar)
        {

            HttpWebRequest webRequest = (HttpWebRequest)ar.AsyncState;

            Stream postStream = webRequest.EndGetRequestStream(ar);

            (using (StreamWriter sw = new StreamWriter(postStream))
            {
                 sw.Write(postData);
                // postData is in json format like: {"Name":"Michel"}  
            }

           postStream.close(); 
            webRequest.BeginGetResponse(new AsyncCallback(SaveBeginGetResponseCallback), webRequest);
        }

        private void SaveBeginGetResponseCallback(IAsyncResult ar)
        {

            try
            {
                HttpWebRequest webRequest = (HttpWebRequest)ar.AsyncState;
                HttpWebResponse response;

                // End the get response operation
                response = (HttpWebResponse)webRequest.EndGetResponse(ar);
                Stream streamResponse = response.GetResponseStream();
                StreamReader streamReader = new StreamReader(streamResponse);
                string Response = streamReader.ReadToEnd();
                streamResponse.Close();
                streamReader.Close();
                response.Close();

            }
            catch (WebException e)
            {
                MessageBox.Show(e.Message);
                // Error treatment

            }

但它显示了错误的请求错误。通过 http 请求发送 json 格式是否正确?

I am updating the db table on force.com using Rest API. And i am posting json data to update db table like this.

     // preparing webrequest
      HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);

      // adding request headers
      request.ContentType = "application/json";
      request.Headers["Authorization"] = "OAuth " + token;
      request.Headers["X-PrettyPrint"] = "1";
      // request method
      request.Method = "PATCH";
       // start the asynchronous operation
                request.BeginGetRequestStream(new AsyncCallback(SaveBeginGetRequestStreamCallBack), request);


 private void SaveBeginGetRequestStreamCallBack(IAsyncResult ar)
        {

            HttpWebRequest webRequest = (HttpWebRequest)ar.AsyncState;

            Stream postStream = webRequest.EndGetRequestStream(ar);

            (using (StreamWriter sw = new StreamWriter(postStream))
            {
                 sw.Write(postData);
                // postData is in json format like: {"Name":"Michel"}  
            }

           postStream.close(); 
            webRequest.BeginGetResponse(new AsyncCallback(SaveBeginGetResponseCallback), webRequest);
        }

        private void SaveBeginGetResponseCallback(IAsyncResult ar)
        {

            try
            {
                HttpWebRequest webRequest = (HttpWebRequest)ar.AsyncState;
                HttpWebResponse response;

                // End the get response operation
                response = (HttpWebResponse)webRequest.EndGetResponse(ar);
                Stream streamResponse = response.GetResponseStream();
                StreamReader streamReader = new StreamReader(streamResponse);
                string Response = streamReader.ReadToEnd();
                streamResponse.Close();
                streamReader.Close();
                response.Close();

            }
            catch (WebException e)
            {
                MessageBox.Show(e.Message);
                // Error treatment

            }

But it is showing bad request error. Is it a right way to send json format over http request.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

罗罗贝儿 2024-11-13 13:14:15

在调用 BeginGetResponse 之前,您不会关闭/处置 postStream ...

另外,请调用

sw.Write(data);

由于数据是字符串。您的调用(传递偏移量和计数)适用于字节数组。您实际上是在调用格式重载 http://msdn .microsoft.com/en-us/library/fd857wct(v=VS.96).aspx

You are not closing/disposing the postStream prior to calling BeginGetResponse ...

Also, call

sw.Write(data);

Since data is a string. Your call (passing an offset and a count) would be appropriate for a byte array. You are actually calling a formatting overload http://msdn.microsoft.com/en-us/library/fd857wct(v=VS.96).aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文