如何基本验证头添加到的WebRequest [英] How to add basic authentication header to WebRequest

查看:186
本文介绍了如何基本验证头添加到的WebRequest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基本的WCF服务,我想用的HttpWebRequest进行测试。问题是,我使用基本身份验证。如何添加页眉与基本身份验证?

I have a basic WCF service and I want to test it using HttpWebRequest. The problem is that I use basic authentication. How do I add a header with basic authentication?

这是我的code到目前为止:

That's my code so far:

var request = (HttpWebRequest)WebRequest.Create(url);

感谢

推荐答案

容易。为了将基本验证添加到您的Htt prequest你这样做:

Easy. In order to add a basic authentication to your HttpRequest you do this:

string username = "Your username";
string password = "Your password";

string svcCredentials = Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(username + ":" + password));

request.Headers.Add("Authorization", "Basic " + svcCredentials);

在基本认证,你需要使用的Base64为en code中的凭据。

In basic authentication you need to use Base64 to encode the credentials.

这篇关于如何基本验证头添加到的WebRequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆