【发布时间】:2019-11-26 01:35:14
【问题描述】:
我是web service 和API 的新手,并尝试使用post 方法从URL 获取响应并将参数传递给它。我正在开发一个 C# winform 应用程序,它向这个api 发送请求,并且必须以 JSON 格式返回输出。下面是我的代码,所以我只得到一个 OK 响应而不是实际的 JSON 数据。
private void button1_Click(object sender, EventArgs e)
{
string postData = "station=sub";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
Uri target = new Uri("http://apijsondata/tz_api/");
WebRequest myReq = WebRequest.Create(target);
myReq.Method = "POST";
myReq.ContentType = "application/x-www-form-urlencoded";
myReq.ContentLength = byteArray.Length;
using (var dataStream = myReq.GetRequestStream())
{
dataStream.Write(byteArray, 0, byteArray.Length);
}
using (var response = (HttpWebResponse)myReq.GetResponse())
{
//Do what you need to do with the response.
MessageBox.Show(response.ToString());
}
}
【问题讨论】: