HttpWebRequest 身份验证不起作用
HttpWebRequest Authentication not working
好的,我正在编写一个程序来访问 RESTful 服务,但我首先必须向网站提供身份验证。我有有效的凭据,我的整个代码如下所示:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Net;
using System.IO;
namespace AccessAPI
{
class Program
{
static void Main(string[] args)
{
string uri = "http://domainName/";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
string auth = CreateAuthorization("http://domainName/", "my\username", "password");
request.Headers["Authorization"] = "Basic " + auth;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
Console.WriteLine(reader.ReadToEnd());
}
static string CreateAuthorization(string realm, string userName, string password)
{
string auth = ((realm != null) && (realm.Length > 0) ?
realm + @"\" : "") + userName + ":" + password;
auth = Convert.ToBase64String(Encoding.Default.GetBytes(auth));
return auth;
}
}
}
到目前为止,我使用了其他论坛上发布的一些解决方案。虽然这似乎对某些人有效,但对我而言却并非如此。首先,我知道凭据是准确的。我可以通过网络浏览器输入它们,它们的行为就像它们应该的那样。另外,我确保其中的任何“\”(用户名中有一个)在代码中都用“\\”表示。不过,每次我 运行 这个,我都会继续回来 "The remote server returned an error: (401) Unauthorized."
谁能帮我解决这个问题?感谢您的宝贵时间!
从上面的代码中,当 "auth" 字符串被组成时,auth 字符串将如下所示:“http://domainName/:my\username:password”
授权字符串的格式应该是:domainName\username:密码(域名是可选的)。
感谢 Gusman 和 Prashant,你们的评论帮助我朝着正确的方向前进并最终取得成功。所以 Gusman,你是对的,这不仅仅是任何身份验证标准,它使用的是 Window 的 IIS 服务。谷歌搜索如何使用 HttpWebRequest 做到这一点让我构建了这个:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Net;
using System.IO;
namespace AccessAPI
{
class Program
{
static void Main(string[] args)
{
string uri = "http://domain.name/";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "GET";
request.UseDefaultCredentials = false;
request.PreAuthenticate = true;
request.Credentials = new NetworkCredential("username", "password");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
Console.WriteLine(reader.ReadToEnd());
}
}
}
现在我不知道为什么这行得通而其他尝试却行不通。但这一切的症结似乎只是基于这条线
request.Credentials = new NetworkCredential("username", "password");
顺便说一句,Prashant,当我不使用整个 http://domain.name/ 时,它会起作用,就像你说的那样。
好吧,希望如果其他人遇到完全相同的问题,他们将能够找到这个线程并解决问题。
好的,我正在编写一个程序来访问 RESTful 服务,但我首先必须向网站提供身份验证。我有有效的凭据,我的整个代码如下所示:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Net;
using System.IO;
namespace AccessAPI
{
class Program
{
static void Main(string[] args)
{
string uri = "http://domainName/";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
string auth = CreateAuthorization("http://domainName/", "my\username", "password");
request.Headers["Authorization"] = "Basic " + auth;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
Console.WriteLine(reader.ReadToEnd());
}
static string CreateAuthorization(string realm, string userName, string password)
{
string auth = ((realm != null) && (realm.Length > 0) ?
realm + @"\" : "") + userName + ":" + password;
auth = Convert.ToBase64String(Encoding.Default.GetBytes(auth));
return auth;
}
}
}
到目前为止,我使用了其他论坛上发布的一些解决方案。虽然这似乎对某些人有效,但对我而言却并非如此。首先,我知道凭据是准确的。我可以通过网络浏览器输入它们,它们的行为就像它们应该的那样。另外,我确保其中的任何“\”(用户名中有一个)在代码中都用“\\”表示。不过,每次我 运行 这个,我都会继续回来 "The remote server returned an error: (401) Unauthorized." 谁能帮我解决这个问题?感谢您的宝贵时间!
从上面的代码中,当 "auth" 字符串被组成时,auth 字符串将如下所示:“http://domainName/:my\username:password”
授权字符串的格式应该是:domainName\username:密码(域名是可选的)。
感谢 Gusman 和 Prashant,你们的评论帮助我朝着正确的方向前进并最终取得成功。所以 Gusman,你是对的,这不仅仅是任何身份验证标准,它使用的是 Window 的 IIS 服务。谷歌搜索如何使用 HttpWebRequest 做到这一点让我构建了这个:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Net;
using System.IO;
namespace AccessAPI
{
class Program
{
static void Main(string[] args)
{
string uri = "http://domain.name/";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "GET";
request.UseDefaultCredentials = false;
request.PreAuthenticate = true;
request.Credentials = new NetworkCredential("username", "password");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
Console.WriteLine(reader.ReadToEnd());
}
}
}
现在我不知道为什么这行得通而其他尝试却行不通。但这一切的症结似乎只是基于这条线
request.Credentials = new NetworkCredential("username", "password");
顺便说一句,Prashant,当我不使用整个 http://domain.name/ 时,它会起作用,就像你说的那样。 好吧,希望如果其他人遇到完全相同的问题,他们将能够找到这个线程并解决问题。