我想要求第三方提交报告,他们需要通过POST进行“基本访问身份验证”:
Your client application must use Basic Access Authentication to send the user name and password.
有人能指出我正确的方向吗?
编辑:我确实看到this post,但有两个答案,我不确定那是我需要做什么或哪一个是首选方法。
答案 0 :(得分:6)
假设您使用WebRequest,请在您的请求中附加CredentialCache:
NetworkCredential nc = new NetworkCredential("user", "password");
CredentialCache cc = new CredentialCache();
cc.Add("www.site.com", 443, "Basic", nc);
WebRequest request = WebRequest.Create("https://www.site.com");
request.Credentials = cc;
request.PreAuthenticate = true;
request.Method = "POST";
// fill in other request properties here, like content
WebResponse respose = request.GetResponse();
答案 1 :(得分:2)
基本要点是这样的:
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Method = WebRequestMethods.Http.Get;
request.Credentials = new NetworkCredential(username, password);
但有时使用请求凭据时会出现问题,另一种方法是在请求标头中添加身份验证数据
string authInfo = username + ":" + password;
authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
request.Headers["Authorization"] = "Basic " + authInfo;
有关详细信息,请参阅此博客文章
http://charlie.cu.cc/2012/05/how-use-basic-http-authentication-c-web-request/