繁体   English   中英

使用基本身份验证C#登录网站

[英]Log in to a web site using Basic authentication C#

我正在尝试编写一种使用基本身份验证方法访问网站的程序。

站点地址是: http : //sv1.apple-media.in/

我使用以下代码读取网站内容:

String username = "XXXX";
String password = "XXXX";
String url = "http://sv1.apple-media.in/";
WebRequest myReq = WebRequest.Create(url);
CredentialCache mycache = new CredentialCache();
mycache.Add(new Uri(url), "Basic", new NetworkCredential(username, password));
myReq.Credentials = mycache;
myReq.GetResponse();

但是我总是会收到这个错误:

System.Net.WebException was unhandled
   HResult=-2146233079
   Message=The remote server returned an error: (401) Unauthorized.
   Source=System
   StackTrace:
      at System.Net.HttpWebRequest.GetResponse()
      at IDMDownloadListAdderPlugin.Form1.button1_Click(Object sender, EventArgs e) in c:\Users\amir\Documents\Visual Studio 2012\Projects\IDMDownloadListAdderPlugin\IDMDownloadListAdderPlugin\Form1.cs:line 60
      at System.Windows.Forms.Control.OnClick(EventArgs e)
      at System.Windows.Forms.Button.OnClick(EventArgs e)
      at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
      at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
      at System.Windows.Forms.Control.WndProc(Message& m)
      at System.Windows.Forms.ButtonBase.WndProc(Message& m)
      at System.Windows.Forms.Button.WndProc(Message& m)
      at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
      at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
      at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
      at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
      at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
      at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
      at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
      at System.Windows.Forms.Application.Run(Form mainForm)
      at IDMDownloadListAdderPlugin.Program.Main() in c:\Users\amir\Documents\Visual Studio 2012\Projects\IDMDownloadListAdderPlugin\IDMDownloadListAdderPlugin\Program.cs:line 19
      at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
      at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
      at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
      at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
      at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
      at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
      at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
      at System.Threading.ThreadHelper.ThreadStart()
   InnerException:

当我使用Chrome浏览器登录网站时,标题为:

Remote Address:31.3.247.107:80
Request URL:http://sv1.apple-media.in/
Request Method:GET
Status Code:200 OK
Request Headersview source
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8,fa;q=0.6
Authorization:Basic something
Cache-Control:max-age=0
Connection:keep-alive
Host:sv1.apple-media.in
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)                 Chrome/38.0.2125.111 Safari/537.36
Response Headersview source
Connection:keep-alive
Content-Encoding:gzip
Content-Type:text/html
Date:Tue, 11 Nov 2014 03:23:28 GMT
Server:nginx
Transfer-Encoding:chunked
Vary:Accept-Encoding

有人可以告诉我我做错了吗?

谢谢!

您可以自己添加标题,而不是使用NetworkCredential 有关确切格式,请参见基本身份验证

// fixed encoding, but taken from http://stackoverflow.com/questions/25852551
string username = "Your username";
string password = "Your password";

// http://stackoverflow.com/questions/7242316/what-encoding-should-i-use-for-http-basic-authentication
var ISO_8859_1 = Encoding.GetEncoding("ISO-8859-1");
var svcCredentials = Convert.ToBase64String(ISO_8859_1.GetBytes(username + ":" + password));

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

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM