将数据发布到Web API

时间:2013-06-18 07:35:08

标签: wpf api

我一直在尝试将数据发送到Web API VIA帖子。但它似乎没有发送它。

我是这样做的。

 var baseAddress = "http://192.168.0.103/vchatapi/api/Images?gsmNumber=" + profileNumberLbl.Content + "&content=" + base64 + "&contentType=image/" + contentType;
 var http = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(new System.Uri(baseAddress));
  http.Accept = "application/json";
  http.ContentType = "application/json";
  http.Method = "POST";

此代码适用于get:

var baseAddress = "http://192.168.0.103/vchatapi/api/SendSMSVerificationCode?gsmNumber=" + areCode + mobile + "&udid=123456";
var http = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(new System.Uri(baseAddress));
http.Accept = "application/json";
http.ContentType = "application/json";
http.Method = "GET";

try
                    {
                        var response = http.GetResponse();

                        var stream = response.GetResponseStream();
                        var sr = new StreamReader(stream);
                        var content = sr.ReadToEnd();

                        verificationCode = verificationCode.FromJson(content);
                        if (!verificationCode.Equals(""))
                        {
                            MessageBox.Show(this, "Verification Code: " + verificationCode);
                            verificationTextBox.IsEnabled = true;
                            areaCodeCB.IsEnabled = false;
                            mobileNumberTB.IsEnabled = false;
                        }
                        else
                        {
                            MessageBox.Show(this, "Invalid Number");
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(this, ex.Message);
                    }

有什么想法吗?谢谢!

1 个答案:

答案 0 :(得分:0)

由于您正在执行POST,因此您将在请求正文中发送内容。您需要获取请求的流并将数据写入其中。以下答案帖子有一个非常简洁的例子:

https://stackoverflow.com/a/2551006/1184056