c# - 指定的值包含无效的 HTTP header 字符

标签 c# asp.net restsharp

我正在尝试从 ASP.NET 站点通过他们的 REST API 访问 WOWZA Streaming Cloud,但是在 RESTSharp 方面经验不足。

这是我尝试创建的 curl 示例:

curl -H 'wsc-api-key: KEY' -H 'wsc-access-key: KEY' -H 'Content-Type:
application/json' -X POST -d '{"live_stream": {"name": "MyNewLiveStream",
"transcoder_type": "transcoded", "billing_mode": "pay_as_you_go",
"broadcast_location": "us_west_california", "encoder": "other_rtmp",
"delivery_method": "push", "aspect_ratio_width": 1920,
"aspect_ratio_height": 1080}}'https://api.cloud.wowza.com/api/v1/live_streams/

参见:https://sandbox.cloud.wowza.com/apidocs/v1/

这是我使用的 C# 代码:

var client = new RestClient("https://api.cloud.wowza.com/");
        var newRequest = new RestRequest("api/v1/live_streams",Method.POST);
        newRequest.AddHeader("wsc-api-key", ConfigurationManager.AppSettings["WowzaAPIKey"]);
        newRequest.AddHeader("wsc-access-key", ConfigurationManager.AppSettings["WowzaAccessKey"]);
        newRequest.AddHeader("Content-Type", "application/json");
        var body = "{\"live_stream\": {" +
            "\"aspect_ratio_height\": 720," +
            "\"aspect_ratio_width\": 1280," +
            "\"billing_mode\": \"pay_as_you_go\"," +
            "\"broadcast_location\": \"us_west_california\"," +
            "\"closed_caption_type\": \"none\"," +
            "\"delivery_method\": \"push\"," +
            "\"encoder\": \"wowza_gocoder\"," +
            "\"hosted_page\": true," +
            "\"hosted_page_sharing_icons\": true," +
            "\"name\": \"MyLiveStream\"," +
            "\"player_countdown\": false," +
            "\"player_responsive\": true," +
            "\"player_type\": \"original_html5\"," +
            "\"player_width\": 0," +
            "\"recording\": false," +
            "\"target_delivery_protocol\": \"hls\"," +
            "\"transcoder_type\": \"transcoded\"," +
            "\"use_stream_source\": false}";

        newRequest.AddJsonBody(body);
        IRestResponse myResponse = client.Execute(newRequest);

根据响应修改后,响应码为

"{\"meta\":{\"status\":401,\"code\":\"ERR-401-InvalidApiKey\",\"title\":\"Invalid Api Key Error\",\"message\":\"Invalid API key.\",\"description\":\"\",\"links\":[]}}"

最佳答案

你的 header 是错误的 - 你不需要在值中重复内容类型并从 api-key 名称中删除 ::

 newRequest.AddParameter("content-type", "application/json"); 
 newRequest.AddParameter("wsc-api-key", ConfigurationManager.AppSettings["WowzaAPIKey"]);

随着 OP 更新问题而更新

所以现在修改后,调用正常了。当前错误是关于您传递的无效 api key - 可能是您传递的访问 key /api key 无效/已过期。

关于c# - 指定的值包含无效的 HTTP header 字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41870400/

相关文章:

c# - 为什么我不能在这种情况下设置不透明度?

asp.net - .ashx 中的 Server.UrlEncode

c# - 调用 REST 服务一直失败(使用 RESTSharp)

c# - 格式化 json 字符串并将其传递给带有参数的正文会出错

c# - Entity Framework 继承

c# - .aspx 代码错误 "Element ' xxxx' 不受支持。”

c# - HttpClient的授权

http-headers - 从 RestSharp 响应头中获取值

c# Math.Sqrt 实现

asp.net - C# ASP.NET 线程安全静态只读字段