编辑:我想回来需要注意的是,问题并不在我结束所有,而是与另一家公司的一面code
我试图拉起使用基本身份验证的页面。我不断收到一个404页没有发现错误。我可以复制和粘贴我的网址到浏览器中,它工作正常(如果我没有登录到它会弹出一个凭证中他们已经部位,否则会打开什么,我想它来打开)。我一定是在正确的地方和认证,因为我得到一个401(未经过验证错误),如果我intentially放在一个错误的用户名/密码,我得到一个内部服务器错误500,如果我传递一个错误的参数在查询字符串。我已经使用Web客户端尝试和HttpWebRequest的两个领先同一个404未找到错误。
使用Web客户端:
字符串的URL =MyValidURLwithQueryString;
Web客户端的客户端=新的Web客户端();
字符串username =名为myusername;
字符串password =输入mypassword;
字符串凭证= Convert.ToBase64String(Encoding.ASCII.GetBytes(用户名+:+密码));
client.Headers [Htt的prequestHeader.Authorization] =基本+证书;
VAR的结果= client.DownloadString(URL);
回复于(结果);
使用HttpWebRequest的
HttpWebRequest的要求=(HttpWebRequest的)WebRequest.Create(MyValidURL);
字符串AUTHINFO =用户名:密码;
AUTHINFO = Convert.ToBase64String(Encoding.Default.GetBytes(AUTHINFO));
request.Headers.Add(授权,基本+ AUTHINFO);
request.Credentials =新的NetworkCredential(用户名,密码);
request.Method = WebRequestMethods.Http.Get;
request.AllowAutoRedirect = TRUE;
request.Proxy = NULL;
HttpWebResponse响应=(HttpWebResponse)request.GetResponse();
流流= response.GetResponseStream();
StreamReader的StreamReader的=新的StreamReader(流);
字符串s = streamreader.ReadToEnd();
回复于(S);
解决方案
//当心
//这个只有当服务器返回401首
//客户端不发出的第一个请求凭据
//只有经过401
client.Credentials =新的NetworkCredential(用户名,密码); //不工作
//所以用这个来代替发送凭据马上
字符串凭证= Convert.ToBase64String(
Encoding.ASCII.GetBytes(用户名+:+密码));
client.Headers [Htt的prequestHeader.Authorization] =的String.Format(
基本{0},证书);
Edit: I wanted to come back to note that the problem wasn't on my end at all, but rather with with code on the other company's side.
I'm trying to pull up a page using Basic Authentication. I keep getting a 404 Page not found error. I can copy and paste my url into the browser and it works fine (if I'm not logged into their site already it pops up a credential box, otherwise it opens what I want it to open). I must be getting to the right place and authenticating, because I get a 401 (not authenticated error) if I intentially put in a bad username/password and I get an internal server error 500 if I pass it a bad parameter in the query string. I've tried using Webclient and HttpWebRequest both leading to the same 404 not found error.
With Webclient:
string url = "MyValidURLwithQueryString";
WebClient client = new WebClient();
String userName = "myusername";
String passWord = "mypassword";
string credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes(userName + ":" + passWord));
client.Headers[HttpRequestHeader.Authorization] = "Basic " + credentials;
var result = client.DownloadString(url);
Response.Write(result);
With HttpWebRequest
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("MyValidURL");
string authInfo = "username:password";
authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
request.Headers.Add("Authorization", "Basic " + authInfo);
request.Credentials = new NetworkCredential("username", "password");
request.Method = WebRequestMethods.Http.Get;
request.AllowAutoRedirect = true;
request.Proxy = null;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream stream = response.GetResponseStream();
StreamReader streamreader = new StreamReader(stream);
string s = streamreader.ReadToEnd();
Response.Write(s);
解决方案
//BEWARE
//This works ONLY if the server returns 401 first
//The client DOES NOT send credentials on first request
//ONLY after a 401
client.Credentials = new NetworkCredential(userName, passWord); //doesnt work
//So use THIS instead to send credentials RIGHT AWAY
string credentials = Convert.ToBase64String(
Encoding.ASCII.GetBytes(userName + ":" + password));
client.Headers[HttpRequestHeader.Authorization] = string.Format(
"Basic {0}", credentials);