简体   繁体   English

如何在REST服务的请求标头中设置cookie

[英]How to set cookie in request header for REST service

I am developing webservice which calls REST service and as per requirement I need to set cookie in the request header. 我正在开发调用REST服务的Web服务,并且根据要求,我需要在请求标头中设置cookie。 EVen if I set everything , it gives me "401 Unauthorized message" as response. 如果我设置了所有内容,它会给我“ 401未经授权的消息”作为响应。

 var request = (HttpWebRequest)HttpWebRequest.Create("https://TESTRestAPI/service/1/sub");
 request.Headers.Add("Authorization", "Basic" + Encoded);
 request.Method = "GET";
 request.ContentType = "application/json";
 var response = (WebResponse)request.GetResponse();

So far I tried this :-
1. request.Headers["Cookie"] = "BasicAuth=fromTest";
2. request.CookieContainer = new CookieContainer();
   Uri target = new Uri("https://TESTRestAPI/service/1/sub"););
   Cookie ck = new Cookie("BasicAuth","fromTest") { Domain = target.Host };

This is first time I am calling REST service, any help appreciated. 这是我第一次致电REST服务,感谢您的帮助。

this code is to authenticate an http request by using basic authentication, with some Username and Password 此代码用于通过使用一些用户名和密码的基本身份验证来验证http请求

 byte[] credentialBuffer = new UTF8Encoding().GetBytes(username + ":" + password);
 string authToken = Convert.ToBase64String(credentialBuffer);
 string authHeaderValue = string.Format(System.Globalization.CultureInfo.InvariantCulture, "Basic {0}", authToken);
 HttpWebRequest request = (HttpWebRequest)System.Net.WebRequest.Create("https://TESTRestAPI/service/1/sub");
 request.Headers.Add(HttpRequestHeader.Authorization, authHeaderValue);

Additionaly, if it is required that the request contains some cookie with some name and some value, Id' use your code number 2. 另外,如果要求该请求包含一些具有某些名称和值的cookie,则请使用您的代码2。

 CookieContainer requestCookieContainer = new CookieContainer();
 Cookie requiredCookie = new Cookie("BasicAuth", "fromTest");
 requiredCookie.Domain = "https://TESTRestAPI/service/1/sub";
 requiredCookieContainer.Add(requiredCookie);
 request.CookieContainer = requestCookieContainer;

If your application runs on IIS, the config file may need some lines. 如果您的应用程序在IIS上运行,则配置文件可能需要一些行。 I think basic authentication is disabled by default, as it isn't too much safe. 我认为默认情况下禁用基本身份验证,因为它不太安全。

 <system.webServer>
    <security>
       <authentication>
          <basicAuthentication enabled="true"/>
       </authentication>
    </security>
  </system.webServer> 

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

相关问题 如何在WPF应用程序中的SOAP客户端的“ Set-Cookie”标头响应的新请求中设置“ Cookie”标头 - How do I set “Cookie” header in a new request from “Set-Cookie” header response for a SOAP client in a WPF application GET REQUEST(REST)中的Cookie不在响应的Cookie集合中,而是在标头中 - Cookie in GET REQUEST (REST) not in Cookies collection in response, but in header 设置请求标头SharePoint Web服务 - Set Request Header SharePoint web service 如何使用新的WCF REST HttpClient API设置GoogleLogin请求的Authorization标头 - How do I do set the Authorization header of a GoogleLogin request using the new WCF REST HttpClient API 如何在需要标头Cookie的Powershell中发出SoapUi请求? - how to make SoapUi request in powershell which requires header cookie? 如何使用正则表达式拆分set-cookie标头 - How to use regex to split set-cookie header 设置特定于服务请求HTTP客户端+ REST的代理服务器 - Set proxy server specific to a service request HTTP client+REST 如何设置WCF服务以检查SOAP标头 - How to set WCF service to check SOAP Header Winform应用程序调用服务时如何设置Cookie - How to set cookie when winform application calls service Cookie请求后如何将瑞典字母设置为原始状态 - How to set Swedish letters to their original state after a cookie request
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM