我难以与使用基本身份验证来验证用户的网站进行交互。
我正在研究visual basic,我已经尝试过使用
req.Headers.Add("Authorization: Basic " & Convert.ToBase64String(Encoding.Default.GetBytes("user" & ":" & "password")))
将标题直接添加到网络请求中:
req.Credentials = New Net.NetworkCredential("user", "password")
或使用网络凭据:
Host: url.to.website.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: it-IT,it;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Referer: http.//url.to.website.com/portal/data/pub
DNT: 1
Authorization: Basic ZmFrZTpwYXNzd29yZA==
Connection: keep-alive
始终收到相同的响应代码:401 Unauthorized
使用Firefox开发人员工具我可以分析和重新发送一些Web请求,只使用Firefox我能够正确验证。
Firefox报告这些标题:
req.Host = "url.to.website.com"
req.UserAgent = "Mozilla/5.0 (Windows NT 6.1; rv:11.0) Gecko/20100101 Firefox/11.0"
req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
req.Referer = "https://url.to.website.com/some/path/to/file.jsf"
req.ContentType = "application/x-www-form-urlencoded"
req.KeepAlive = True
req.PreAuthenticate = True
req.Method = "POST"
req.Headers.Add("Authorization: Basic " & Convert.ToBase64String(Encoding.Default.GetBytes("user" & ":" & "password")))
所以我试着用这种方式设置它:
WebBrowser1.Navigate("url", Nothing, Nothing, "Authorization: Basic " & Convert.ToBase64String(Encoding.Default.GetBytes(AUTH_USER & ":" & AUTH_PASSWORD)))
没有成功(总是收到相同的响应代码:401 Unauthorized)
另一个尝试是使用网络浏览器:
SELECT t.*
, split_part(o, '.', 1) AS col1
, split_part(o, '.', 2) AS col2
, split_part(o, '.', 3) AS col3
, split_part(o, '.', 4) AS col4
, split_part(o, '.', 5) AS col5
FROM tbl t, translate(t.orig_val, '_', '.') o; -- implicit LATERAL join
我的目标是进行身份验证,然后查询某些页面并收集回复,以便解析它们并在以后的应用程序中使用它。
如何解决有关身份验证的问题?
该网站使用JSF编写,我无法控制它。
更新
我的问题是关于身份验证,还没有关于jsf应用程序。
虽然使用Firefox一切正常(我可以向网站发送请求并且它会正确验证我)但是在使用HttpWebRequest时验证失败,即使我设置了相同的标题,写在之前。
我必须弄清楚两个请求之间的区别
答案 0 :(得分:0)
我必须让这个工作Dukes Forest Java EE Tutorial Port to Wildfly。代码已经编写,但标题区分大小写。无论如何,那里使用的代码如下:
/* Client filter for basic HTTP auth */
class AuthClientRequestFilter implements ClientRequestFilter {
private final String user;
private final String password;
public AuthClientRequestFilter(String user, String password) {
this.user = user;
this.password = password;
}
@Override
public void filter(ClientRequestContext requestContext) throws IOException {
try {
requestContext.getHeaders().add(
"Authorization",
"Basic " + DatatypeConverter.printBase64Binary(
(user+":"+password).getBytes("UTF-8"))
);
} catch (UnsupportedEncodingException ex) { }
}
}
从javax.xml.bind导入DatatypeConverter。此代码是从以下例程调用的,该例程具有HTTPClient:
Client client = ClientBuilder.newClient();
client.register(new AuthClientRequestFilter("jack@example.com", "1234"));
Response resp = client.target(ENDPOINT)
.request(MediaType.APPLICATION_XML)
.post(Entity.entity(order, MediaType.APPLICATION_XML), Response.class);
int status = resp.getStatus();
if (status == 200) {
success = true;
}
logger.log(Level.INFO, "[PaymentHandler] Response status {0}", status);
client.close();
return success;
此客户端代码发布到RESTful服务。