简体   繁体   English

连接到C#中的Web服务

[英]Connecting to web service in C#

I am trying to connect to a web service from Lockheed Martin located here . 我正在尝试从位于此处的 Lockheed Martin连接到Web服务。 I have looked at other examples and am using the following code to try and establish a connection. 我看了其他示例,并使用以下代码尝试建立连接。 All I want to know at this point is if I have established a connection and been authorized but I repeatedly get an exception saying 我现在想知道的是,我是否已建立连接并获得授权,但是我反复得到一个异常提示

Unauthorized at System.Net.HttpWebRequest.GetResponse() 未经授权的System.Net.HttpWebRequest.GetResponse()

. Am I setting up the web request and response correctly? 我是否正确设置了Web请求和响应? Is there a different method that would simply let me know if I've successfully connected? 是否有其他方法可以简单地让我知道是否已成功连接?

            try
        {
            //Connect to the Lockheed Martin web client

            WebRequest client = WebRequest.Create("https://www.elabs.testafss.net/Website2/ws");
            string username = "username";
            string password = "password";
            string credentials = Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(username + ":" + password));
            client.Headers.Add("Authorization", "Basic " + credentials);
            WebResponse response = client.GetResponse();
            Console.WriteLine(((HttpWebResponse)response).StatusDescription);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }

Probably your user / password is incorrect, looking at the documentation of the web service, your code is reproducing the exactly same hash that is in following sample: 您的用户名/密码可能不正确,请查看该Web服务的文档,您的代码正在再现以下示例中完全相同的哈希:

Authentication - Basic Auth 身份验证-基本身份验证

Authentication is performed using the Basic Auth protocol. 使用基本身份验证协议执行身份验证。 An authorization header is supplied with every web service request. 每个Web服务请求都提供了一个授权标头。 This is sometimes called pre-emptive authentication. 有时称为抢先验证。 The header looks like this: 标头看起来像这样:
Authorization: Basic Vendor_ID:Vendor_Password
where the Vendor_ID:Vendor_Password string is converted to Base64. Vendor_ID:Vendor_Password字符串转换为Base64的位置。

Example

Authorization: Basic JoesFlightServices:SecretPW
Converted to Base64: 转换为Base64:
Authorization: Basic Sm9lc0ZsaWdodFNlcnZpY2VzOlNlY3JldFBX
Note that conversion to Base64 does not ensure the information will be private. 请注意,转换为Base64不能确保该信息是私有的。 We use HTTPS to encrypt the entire HTTP message including the headers. 我们使用HTTPS加密包括头的整个HTTP消息。

Source 资源

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

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