使用基本身份验证的HttpWebRequest


137

我正在尝试通过一个模仿“基本身份验证请求”的身份验证请求,该请求是我们在为此行为设置IIS时经常会看到的。

该URL是:https
://telematicoprova.agenziadogane.it/TelematicoServiziDiUtilitaWeb/ServiziDiUtilitaAutServlet?UC =22&SC=1&ST=2(警告:https!)

该服务器作为应用程序服务器在UNIX和Java下运行。

这是我用来连接到该服务器的代码:

CookieContainer myContainer = new CookieContainer();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://telematicoprova.agenziadogane.it/TelematicoServiziDiUtilitaWeb/ServiziDiUtilitaAutServlet?UC=22&SC=1&ST=2");
request.Credentials = new NetworkCredential(xxx,xxx);
request.CookieContainer = myContainer;
request.PreAuthenticate = true;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

(我从本网站的另一篇文章中复制了此内容)。但是我从服务器收到以下答案:

基础连接已关闭:发送时发生意外错误。

我想我已经尽了我所有的C#知识所能提供的一切可能的任务,但是没有什么...


我认为,如果您添加以下代码,该代码就可以工作:request.UseDefaultCredentials = false;
bpeikes 2014年

Answers:


273

您也可以自己添加授权标头。

只需输入名称“ Authorization”和值“ Basic BASE64({USERNAME:PASSWORD})”即可。

String username = "abc";
String password = "123";
String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(username + ":" + password));
httpWebRequest.Headers.Add("Authorization", "Basic " + encoded);

编辑

将编码从UTF-8切换为ISO 8859-1,对于HTTP基本身份验证应使用哪种编码?和耶隆的评论。


3
那么,您如何确定UTF8是正确使用的编码呢?
Jeroen Wiert Pluimers 2014年

2
不错的收获。看起来请求Authentication标头应以ISO-8859-1编码。每stackoverflow.com/questions/7242316/...
Zambonilli

1
啊!1:谢谢,队友 2.我真的很想了解为什么其他方法(在CredentialCache中设置身份验证方法)根本不起作用。他们应该,不是吗?
mshthn

1
所有的msdn文档都指向yes,其他方法应该起作用。但是,我从来没有让他们工作。
Zambonilli

对我不起作用(utf-8或ISO-8859-1都不适用)。有什么建议要检查吗?当我执行“ webRequest.Credentials = new NetworkCredential(UserID,Password);”时,它可以工作 。
RollerCosta

56

我终于明白了!

string url = @"https://telematicoprova.agenziadogane.it/TelematicoServiziDiUtilitaWeb/ServiziDiUtilitaAutServlet?UC=22&SC=1&ST=2";
WebRequest request = WebRequest.Create(url);
request.Credentials = GetCredential();
request.PreAuthenticate = true;

这是 GetCredential()

private CredentialCache GetCredential()
{
    string url = @"https://telematicoprova.agenziadogane.it/TelematicoServiziDiUtilitaWeb/ServiziDiUtilitaAutServlet?UC=22&SC=1&ST=2";
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
    CredentialCache credentialCache = new CredentialCache();
    credentialCache.Add(new System.Uri(url), "Basic", new NetworkCredential(ConfigurationManager.AppSettings["ead_username"], ConfigurationManager.AppSettings["ead_password"]));
    return credentialCache;
}

好极了!


29

如果可以使用WebClient该类,则使用基本身份验证将变得很简单:

var client = new WebClient {Credentials = new NetworkCredential("user_name", "password")};
var response = client.DownloadString("https://telematicoprova.agenziadogane.it/TelematicoServiziDiUtilitaWeb/ServiziDiUtilitaAutServlet?UC=22&SC=1&ST=2");

8

试试这个:

System.Net.CredentialCache credentialCache = new System.Net.CredentialCache(); 
credentialCache.Add(
    new System.Uri("http://www.yoururl.com/"),
    "Basic", 
    new System.Net.NetworkCredential("username", "password")
);

...
...

httpWebRequest.Credentials = credentialCache; 

一定是基本身份验证吗?另外,您可以通过浏览器使用用户名/密码访问资源吗?
MrEyes

它似乎是通过https进行的基本身份验证。如果您单击我提供的链接,浏览器会弹出与在IIS上执行“基本身份验证”或通过apache在文件夹中使用.htaccss文件时相同的用户名/密码请求。我一无所知
肯尼·鲁洛

我刚刚了解到该网站正在IBM http服务器下运行。这可能对您有帮助吗?
肯尼·鲁洛

5

对于使用这些RestSharp,它可能使用时失败SimpleAuthenticator(不使用ISO-8859-1幕后可能是由于)。我设法通过显式发送基本身份验证标头来完成此操作:

string username = "...";
string password = "...";

public IRestResponse GetResponse(string url, Method method = Method.GET)
{
    string encoded = Convert.ToBase64String(Encoding.GetEncoding("ISO-8859-1").GetBytes($"{username}:{password}"));
    var client = new RestClient(url);
    var request = new RestRequest(method );
    request.AddHeader("Authorization", $"Basic {encoded}");
    IRestResponse response = client.Execute(request);
    return response;
}

var response = GetResponse(url);
txtResult.Text = response.Content;

3

如果实现了基本身份验证和代理,则以下代码将解决json响应。还将解决IIS 7.5 Hosting Problm。

public string HttpGetByWebRequ(string uri, string username, string password)
{
//For Basic Authentication
    string authInfo = username + ":" + password;
    authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));

//For Proxy
    WebProxy proxy = new WebProxy("http://10.127.0.1:8080", true);

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
    request.Method = "GET";
    request.Accept = "application/json; charset=utf-8";
    request.Proxy = proxy;

    request.Headers["Authorization"] = "Basic " + authInfo;

    var response = (HttpWebResponse)request.GetResponse();

    string strResponse = "";
    using (var sr = new StreamReader(response.GetResponseStream()))
    {
        strResponse= sr.ReadToEnd();

    }

    return strResponse;
}

不适用于我(utf-8或ISO-8859-1均非默认值)。有什么建议可以检查吗?请注意-当我执行“ webRequest.Credentials = new NetworkCredential(UserID,Password);”时,此方法有效
RollerCosta


1

以下构造对我而言无法正常工作:

request.Credentials = new NetworkCredential("user", "pass");

CredentialCache改用了:

CredentialCache credentialCache = new CredentialCache
{
    {
        new Uri($"http://{request.Host}/"), "Basic",
        new NetworkCredential("user", "pass")
    }
};
request.Credentials = credentialCache;

但是,如果您想添加多个基本身份验证凭据(例如,如果您知道重定向),则可以使用以下功能:

private void SetNetworkCredential(Uri uriPrefix, string authType, NetworkCredential credential)
{
    if (request.Credentials == null)
    {
        request.Credentials = new CredentialCache();
    }

    if (request.Credentials.GetCredential(uriPrefix, authType) == null)
    {
        (request.Credentials as CredentialCache).Add(uriPrefix, authType, credential);
    }
}

我希望它将对将来的人有所帮助。


0

首先,对我而言,ServicePointManager.SecurityProtocol = SecurityProtocolType。Tls12代替了Ssl3。

其次,我必须发送基本身份验证请求以及一些数据(采用表单编码)。在尝试了许多解决方案之后,这是为我完美工作的完整示例。

免责声明:以下代码是在此链接和一些其他stackoverflow链接上找到的解决方案的混合,感谢有用的信息。

        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
        String username = "user_name";
        String password = "password";
        String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(username + ":" + password));

        //Form Data
        var formData = "var1=val1&var2=val2";
        var encodedFormData = Encoding.ASCII.GetBytes(formData);

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("THE_URL");
        request.ContentType = "application/x-www-form-urlencoded";
        request.Method = "POST";
        request.ContentLength = encodedFormData.Length;
        request.Headers.Add("Authorization", "Basic " + encoded);
        request.PreAuthenticate = true;

        using (var stream = request.GetRequestStream())
        {
            stream.Write(encodedFormData, 0, encodedFormData.Length);
        }

        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.