简体   繁体   English

为什么后操作在 Postman 中有效,而不在 C# 中有效

[英]Why does Post action work in Postman, not in C#

I am trying to execute a GET command so I get can get data from a server.我正在尝试执行 GET 命令,以便可以从服务器获取数据。 The Curl below works in Postman.下面的 Curl 在 Postman 中工作。

$ curl -X GET -H "Authorization: {SESSION_ID}" \
https://{server}/api/{version}/metadata/vobjects

While running my code, I am able to get the session ID.在运行我的代码时,我可以获得 session ID。 The next step is to get the data.下一步是获取数据。 But when I execute the GET, I don't get any response.但是当我执行 GET 时,我没有得到任何响应。 Instead a I get an error as follows: "Specified value has invalid HTTP Header characters. (Parameter 'name')"相反,我收到如下错误:“指定的值具有无效的 HTTP Header 字符。(参数'名称')”

Below is the C# code I am trying to execute下面是我试图执行的 C# 代码

public static void getObjects(string sessionID)
        {
            var client = new RestClient("https://generic-server.com/api/v20.3/metadata/vobjects/");
            client.Timeout = -1;
            var request = new RestRequest(Method.GET);
            request.AddHeader("Authorization:", sessionID);
            IRestResponse response = client.Execute(request);
            Console.WriteLine("Here's the response: "  + response.Content + " " + response.Content.Length);
            Console.WriteLine("Here's the error message: " + response.ErrorMessage);
        }

Problem: I get no response from the server and the response length is zero.问题:我没有得到服务器的响应,响应长度为零。

Here's the response: 0回复如下:0

response.ErrorMessage gives the following: response.ErrorMessage 给出以下内容:

Here's the error message: Specified value has invalid HTTP Header characters.这是错误消息:指定的值具有无效的 HTTP Header 字符。 (Parameter 'name') (参数“名称”)

Note: When I leave the colon out of of the header, then I get the following response from the server:注意:当我将冒号留在 header 之外时,我会从服务器收到以下响应:

{"responseStatus":"FAILURE","errors":[{"type":"INVALID_DATA","message":"Invalid http header [Accept: application/json, text/json, text/x-json, text/javascript, application/xml, text/xml]"}]} 190 {"responseStatus":"FAILURE","errors":[{"type":"INVALID_DATA","message":"Invalid http header [接受:application/json, text/json, text/x-json, text/ javascript, 应用程序/xml, 文本/xml]"}]} 190

Have you tried ("Authorization", $"Bearer {sessionID"});你试过(“授权”,$“Bearer {sessionID”});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM