【发布时间】:2019-12-27 04:46:32
【问题描述】:
我在我的 Mac 上的 Visual Studio 2019 中创建了一个 c# 库。我试图在我的dll 中公开一个方法,该方法应该能够进行http 调用并返回响应。
但我收到错误 Cannot send a content-body with this verb-type。
我对@987654325@ 很陌生,所以我只是从网上复制粘贴代码。请告诉我我做错了什么。
我的图书馆,
using System;
using System.IO;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;
namespace ImageTrainer
{
public class Trainer
{
public static void TrainImage()
{
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)WebRequest.Create("https://www.abcd.com/rest/city/search/Bang");
request.Method = "GET";
request.ContentType = "application/json";
// // request.ContentLength = DATA.Length;
StreamWriter requestWriter = new StreamWriter(request.GetRequestStream(), System.Text.Encoding.ASCII);
// // requestWriter.Write(DATA);
requestWriter.Close();
WebResponse webResponse = request.GetResponse();
Stream webStream = webResponse.GetResponseStream();
StreamReader responseReader = new StreamReader(webStream);
string response = responseReader.ReadToEnd();
Console.WriteLine(response);
// responseReader.Close();
// Console.WriteLine(response);
}
}
}
我正在另一个控制台应用程序中使用该库。
using System;
using ImageTrainer;
namespace ImageTrainerTest
{
class MainClass
{
public static void Main(string[] args)
{
Console.WriteLine("Hello World!");
Trainer.TrainImage();
}
}
}
我犯了什么错误?
【问题讨论】:
-
HTTP
GET请求不能有正文。所以Content-Type标头也没有意义。您可能想要POST。或者不发送尸体。或者您可能想要一个Accept标头告诉服务器您希望响应的正文是 JSON?