【问题标题】:C# POST request to webapi using a console program使用控制台程序向 webapi 发出 C# POST 请求
【发布时间】:2016-07-25 16:14:14
【问题描述】:

我有一个可以通过浏览器成功访问的 web api :-

https://127.0.0.1:8443/ncrApi

我正在尝试使用 VS2015 在 C# 中创建一个简单的控制台程序,以使用 http POST 发送数据和接收响应。

这是我目前所拥有的:-

using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;

namespace WebSample
{

    class ApiSendData
    {
        public string ApiFunction { get; set; }
        public string DppName { get; set; }
        public string ClearData { get; set; }
        public string DppVersion { get; set; }
    }



    class Program
    {
        static void Main(string[] args)
        {
            // The Main function calls an async method named RunAsync 
            // and then blocks until RunAsyncc completes.
            RunAsync().Wait();
        }

        static async Task RunAsync()
        {
            using (var client = new HttpClient())
            {
                // This code sets the base URI for HTTP requests, 
                // and sets the Accept header to "application/json", 
                // which tells the server to send data in JSON format.
                client.BaseAddress = new Uri("https://localhost:8443/ncrApi");
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));


                // HTTP POST
                var datatobeSent = new ApiSendData()
                                    {
                                        ApiFunction ="NcrSecureData",
                                        DppName ="CSampleCustomer",
                                        DppVersion ="Latest",
                                        ClearData ="1234567890"
                                    };

                HttpResponseMessage response = await client.PostAsJsonAsync("ncrApi", datatobeSent);

                if (response.IsSuccessStatusCode)
                {
                    // Get the URI of the created resource.
                    Uri ncrUrl = response.Headers.Location;

                    // do whatever you need to do here with the returned data //


                }
            }
        }
    }
}

但是,我在 HttpResonseMessage 响应语句中遇到错误......{“发送请求时发生错误。”} {“请求中止:无法创建 SSL/TLS 安全通道。”}

我怀疑这是因为我没有正确理解 client.BaseAddress 和 HttpResponseMessage 响应语句。

这是我一直在关注的:- http://www.asp.net/web-api/overview/advanced/calling-a-web-api-from-a-net-client

【问题讨论】:

  • 其他 cmets:- 1) 我关闭了 SSL 并成功连接到端口 8080 上的 web api。2) 我用我的代码成功连接到其他 SSL api。似乎 C# 代码在 SSL 连接上没有连接到 ncrApi。任何想法为什么?

标签: c# api post console http-post


【解决方案1】:

您可能会遇到错误,因为最终地址是您的 baseAddress + 邮寄地址,即:http://localhost:8443/nrcApi/nrcApi,不存在

尝试将您的 client.BaseAddress 更改为:

client.BaseAddress = new Uri("https://localhost:8443/");

对于 SSL 连接错误,请尝试生成可信证书: Make https call using httpclient

【讨论】:

  • 谢谢。我试过了,同样的错误。但是这次我关闭了 SSL。并刚刚连接到 localhost:8080/ncrapi 并且工作正常。我尝试连接到其他 SSL api,我的程序成功连接到它们
  • 您是否已正确配置您的 ncrapi 端点以接受端口 8443 上的传入 SSL 连接?这可能是与您的端点相关的一些服务器端问题。
  • 我刚找到这个,看看:stackoverflow.com/questions/22251689/…也有可能是你没有可信证书...
  • 谢谢!!这解决了我的问题。我现在收到 200 OK 响应。但是没有数据。哈哈,难题还在继续
  • 好吧!你能把它标记为答案吗?我将使用参考进行编辑
【解决方案2】:

您的代码看起来不错。但是,问题似乎是您正在拨打的电话。基本上在下面的调用中,第一个参数是在你的 URI 之后要调用的方法/函数。

HttpResponseMessage 响应 = 等待 client.PostAsJsonAsync("ncrApi", 数据发送);

在这种情况下 ncrApi 是您的方法。在基本 URL 中调用它会导致它被添加到最终调用中,使其为不存在的端点提供地址。

【讨论】:

    猜你喜欢
    • 2023-03-10
    • 2012-06-10
    • 2016-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-13
    相关资源
    最近更新 更多