0

我一直在尝试将数据发送到 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);
                    }

有任何想法吗?谢谢!

4

1 回答 1

0

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

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

于 2013-06-18T08:05:18.493 回答