An example of HttpClient requesting the WCF Service using Windows authentication, httpclientwcf

Source: Internet
Author: User

An example of HttpClient requesting the WCF Service using Windows authentication, httpclientwcf

A third-party SDK needs to be called for a project, and the SDK returns the list of installed special printers of the server to the caller.

But I don't want to rely on this SDK, because this SDK is written using. NET Framework, and my project is written using. NET Core and wants to be deployed and run in a Docker container.

The SDK is decompiled and the source code is viewed. the SDK calls a URL to obtain the result.

This URL starts with a local URL, http: // localhost. Now I know that the software corresponding to this SDK provides a local Web service on the server.

So I removed this SDK from the project and called the URL directly.

But failed to get the result. The message "response status code does not indicate success: 401 (Unauthorized)" is displayed ).". Directly accessing the URL in the browser returns the result.

 

At the beginning, I was overwhelmed. I don't know why there was no authentication...

It is customary to sacrifice the Fiddler to listen to this URL, directly access this URL in the browser, but in Fiddler, it is seen that this URL has been requested three times.

I didn't care at the time. I directly got the Request Header, inserted it into the HttpClient Header, and returned an error again.

After a rough look at Fiddler, I noticed the issue of repeated requests three times.

I thought it was a request redirection, set AllowAutoRedirect to true, and then request again, and report an error.

Again, read Fiddler carefully one by one. The second line shows Authorization: Negotiate xxxxxxxxx in the Request Header.

 

This means that Authorization authentication is used, so I checked the port Occupation List of Windows based on the URL port and found the PID corresponding to the port occupation.

Find the PID and find the process. Then, find the folder directory where the process is located and view its config configuration file. From the configuration description, this Web service is a more accurate WCF Service.

It uses the security node:

<security mode="TransportCredentialOnly">  <transport clientCredentialType="Windows"></transport></security>

I set usedefacrecredentials to true and request again. The result is obtained successfully.

This is the final code for HttpClient to request the local WCF Service:

Private static async void Test () {Random rand = new Random (); var r = rand. next (10000,999 99); string url = "http: // localhost: 8080/WebPrintService/GetClientPrinters? Rand = "+ r; var handler = new HttpClientHandler (); // handler. allowAutoRedirect = true; // handler. useDefaultCredentials = true; // handler. preAuthenticate = true; HttpClient httpClient = new HttpClient (handler); // HttpRequestMessage requestMessage = new HttpRequestMessage (); // requestMessage. requestUri = new Uri (url); // requestMessage. method = HttpMethod. get; // requestMessage. headers. cacheControl. maxAge = TimeSpan. zero; // requestMessage. headers. authorization = new AuthenticationHeaderValue (); // requestMessage. headers. accept. clear (); // requestMessage. headers. accept. parseAdd ("application/json, text/javascript, */*; q = 0.01"); // requestMessage. headers. acceptEncoding. clear (); // requestMessage. headers. acceptEncoding. parseAdd ("gzip, deflate"); // requestMessage. headers. acceptLanguage. clear (); // requestMessage. headers. acceptLanguage. parseAdd ("zh-CN"); // requestMessage. headers. userAgent. clear (); // requestMessage. headers. userAgent. parseAdd ("Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv: 11.0) like Gecko"); // requestMessage. headers. add ("X-Requested-With", "XMLHttpRequest"); // var responseMessage = await httpClient. sendAsync (requestMessage); // var result = await responseMessage. content. readAsStringAsync (); var result = await httpClient. getStringAsync (url); JsonConvert. deserializeObject <PrinterInfo> (jsonString );//The second method is provided by. NET Framework. You do not need to use NuGet to introduce Microsoft. Net. Http packets for HttpClient.// WebRequest request = WebRequest. create (url); // request. method = "GET"; // request. usedefacrecredentials = true; // WebResponse response = request. getResponse (); // var stream = response. getResponseStream (); // using (var streamReader = new StreamReader (stream) // {// using (var textReader = new JsonTextReader (streamReader )) // {// var serializer = new JsonSerializer (); // var result = serializer. deserialize <List <PrinterInfo> (textReader );//}//}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.