【问题标题】:How to call https api from wpf如何从 wpf 调用 https api
【发布时间】:2019-08-27 10:45:35
【问题描述】:

我正在尝试从 WPF 应用程序调用 https API,但出现此错误:

InnerException = {"底层连接已关闭:意外 发送时出错。"} Message = "发送时出错 请求。”

谁能帮我看看到底是什么问题?

  private static readonly string apiURL = 
                "https://api.totalsynergy.com/api/v2/Profile/Index";    

  private async Task<bool> GetAuth(string accessToken)
    {
        try
        {
            HttpClient hc = new HttpClient();
            HttpResponseMessage hpm = await hc.GetAsync(apiURL);

            if (hpm.IsSuccessStatusCode)
            {
                var res = await hpm.Content.ReadAsAsync<Organization>();
            }

            return boolValue;
        }
        catch (HttpRequestException e)
        {
            Console.WriteLine("\nException Caught!");
            Console.WriteLine("Message :{0} ", e.Message);
            return boolValue;
        }
    }

【问题讨论】:

    标签: wpf api https


    【解决方案1】:

    什么时候是HTTPS,连接需要一些协议。 在我的示例中,我有一个调用的 API URL,可以发送一些信息并以 JSON 格式接收响应。您可以根据自己的问题进行调整:

    ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
    
                   //get OS version
                    var query = "SELECT version FROM Win32_OperatingSystem";
                    var searcher = new ManagementObjectSearcher(query);
                    var info = searcher.Get().Cast<ManagementObject>().FirstOrDefault();
                    string version = info.Properties["Version"].Value.ToString();
                    int majorVersion = Int32.Parse(version.Substring(0, version.IndexOf(".")));
    
                    //OS version is windows xp or older
                    if (majorVersion < 6)
                    {
                        //tls 1.0
                        ServicePointManager.SecurityProtocol = (SecurityProtocolType)192;
                    }
                    else
                    {
                        //tls 1.1 or tls 1.2
                        ServicePointManager.SecurityProtocol = (SecurityProtocolType)768 | (SecurityProtocolType)3072;
                    }
    
                    //url to send data
                    string url = **YOUR URL**
    
    
                    //create request
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    
                    request.KeepAlive = false;
                    request.Timeout = 240000;
                    request.ProtocolVersion = HttpVersion.Version10;
                    request.Method = **REQUEST METHOd GET/POST**;
    
                    request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5";
    
                    //convert to byte stream
                    byte[] postBytes = Encoding.UTF8.GetBytes(**Text to send  or empty**);
    
    
                    //specify content of request - this example is in JSON
                    request.ContentType = "application/json";
    
                    if (requestMethod != RequestMethods.GET)
                    {
    
                        request.ContentLength = postBytes.Length;
    
                        Stream requestStream = request.GetRequestStream();
    
                        //send
                        requestStream.Write(postBytes, 0, postBytes.Length);
    
                        requestStream.Close();
                    }
    
                    try
                    {
    
                        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                        string result;
                        using (var readers = new StreamReader(response.GetResponseStream()))
                        {
                            return result = readers.ReadToEnd();
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.ToString());
                        return null;
    
                    }
                    finally
                    {
                        request.Abort();
                    }
    

    【讨论】:

      猜你喜欢
      • 2018-02-05
      • 2017-02-11
      • 1970-01-01
      • 2015-02-17
      • 2020-02-19
      • 1970-01-01
      • 2019-12-27
      • 1970-01-01
      • 2013-11-07
      相关资源
      最近更新 更多