我正在尝试创建一个将 url 作为参数并从中返回 GET Response 的函数,它只是在该WebResponse responseObject intialization
行中给我一个错误
例外是An exception of type 'System.Net.ProtocolViolationException' occurred in mscorlib.ni.dll but was not handled in user code
public static async Task<string> get(string url)
{
var request = WebRequest.Create(new Uri(url)) as HttpWebRequest;
request.Method = "GET";
request.ContentType = "application/json";
WebResponse responseObject = await Task<WebResponse>.Factory.FromAsync(request.BeginGetResponse, request.EndGetResponse, request);
var responseStream = responseObject.GetResponseStream();
var sr = new StreamReader(responseStream);
string received = await sr.ReadToEndAsync();
return received;
}