C# .NETCore HttpWebRequest-旧式isn';行不通
在升级到最新的.NetCore之前,我能够运行HttpWebRequest,添加标题和内容类型,并从Twitch中提取JSON文件流。由于升级失败,此功能无法正常工作。每次获取响应流时,我都会收到一个Web异常。twitch没有任何变化,因为它仍然与旧的机器人一起工作。旧代码如下:C# .NETCore HttpWebRequest-旧式isn';行不通,c#,json,httpwebrequest,.net-core,twitch,C#,Json,Httpwebrequest,.net Core,Twitch,在升级到最新的.NetCore之前,我能够运行HttpWebRequest,添加标题和内容类型,并从Twitch中提取JSON文件流。由于升级失败,此功能无法正常工作。每次获取响应流时,我都会收到一个Web异常。twitch没有任何变化,因为它仍然与旧的机器人一起工作。旧代码如下: private const string Url = "https://api.twitch.tv/kraken/streams/channelname"; HttpWebRequest request;
private const string Url = "https://api.twitch.tv/kraken/streams/channelname";
HttpWebRequest request;
try
{
request = (HttpWebRequest)WebRequest.Create(Url);
}
request.Method = "Get";
request.Timeout = 12000;
request.ContentType = "application/vnd.twitchtv.v5+json";
request.Headers.Add("Client-ID", "ID");
try
{
using (var s = request.GetResponse().GetResponseStream())
{
if (s != null)
using (var sr = new StreamReader(s))
{
}
}
}
我做了一些研究,发现我可能需要开始使用HttpClient或HttpRequestMessage。我尝试过这样做,但当添加标题内容类型时,程序会停止并退出。在这里的第一行之后:(使用HttpsRequestMessage时)
您正试图添加
ContentType
头,但您真正想要的是添加Accept
头(您的请求是GET
,ContentType
仅用于包含正文的请求,例如POST
或PUT
)
在.NET Core中,您需要使用HttpClient
,但要正确使用它,您需要利用async
和wait
的使用
这是一个例子:
using System.Net.Http;
using System.Net.Http.Headers;
private const string Url = "https://api.twitch.tv/kraken/streams/channelname";
public static async Task<string> GetResponseFromTwitch()
{
using(var client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/vnd.twitchtv.v5+json"));
client.DefaultRequestHeaders.Add("Client-ID", "MyId");
using(var response = await client.GetAsync(Url))
{
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync(); // here we return the json response, you may parse it
}
}
}
使用System.Net.Http;
使用System.Net.Http.Header;
私有常量字符串Url=”https://api.twitch.tv/kraken/streams/channelname";
公共静态异步任务GetResponseFromTwitch()
{
使用(var client=new HttpClient())
{
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(新的MediaTypeWithQualityHeaderValue(“application/vnd.twitchtv.v5+json”);
client.DefaultRequestHeaders.Add(“客户ID”、“MyId”);
使用(var response=await client.GetAsync(Url))
{
response.EnsureSuccessStatusCode();
return wait response.Content.ReadAsStringAsync();//这里我们返回json响应,您可以解析它
}
}
}
您试图添加ContentType
头,但您真正想要的是添加Accept
头(您的请求是GET
,ContentType
仅用于包含正文的请求,例如POST
或PUT
)
在.NET Core中,您需要使用HttpClient
,但要正确使用它,您需要利用async
和wait
的使用
这是一个例子:
using System.Net.Http;
using System.Net.Http.Headers;
private const string Url = "https://api.twitch.tv/kraken/streams/channelname";
public static async Task<string> GetResponseFromTwitch()
{
using(var client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/vnd.twitchtv.v5+json"));
client.DefaultRequestHeaders.Add("Client-ID", "MyId");
using(var response = await client.GetAsync(Url))
{
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync(); // here we return the json response, you may parse it
}
}
}
使用System.Net.Http;
使用System.Net.Http.Header;
私有常量字符串Url=”https://api.twitch.tv/kraken/streams/channelname";
公共静态异步任务GetResponseFromTwitch()
{
使用(var client=new HttpClient())
{
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(新的MediaTypeWithQualityHeaderValue(“application/vnd.twitchtv.v5+json”);
client.DefaultRequestHeaders.Add(“客户ID”、“MyId”);
使用(var response=await client.GetAsync(Url))
{
response.EnsureSuccessStatusCode();
return wait response.Content.ReadAsStringAsync();//这里我们返回json响应,您可以解析它
}
}
}
谢谢!我不得不更改一些twitch请求URL,但我现在有了它working@FedericoDipuma,您的意思是:request=(HttpWebRequest)WebRequest.Create(Url);不行,谢谢!我不得不更改一些twitch请求URL,但我现在有了它working@FedericoDipuma,您的意思是:request=(HttpWebRequest)WebRequest.Create(Url);无法在上运行。NetCore