几天来我一直在尝试连接到Jira服务器(具有基本身份验证),但是我无法使其正常工作。我总是收到“ 403 Forbidden”错误。同时,我非常绝望,希望有人能帮助我。
示例代码如下:
public class Main
{
private static final String JIRA_URL = "https://jira.mycompany.com/rest/api/latest/project/";
private static String auth = new String(Base64.encode("admin" + ":" + "password"));
private static String headerAuthorization = "Authorization";
final static String headerAuthorizationValue = "Basic " + auth;
final static String headerType = "application/json";
public static void main(String[] args) throws Exception
{
Client client = Client.create();
WebResource webResource = client.resource(JIRA_URL);
ClientResponse response = webResource.header(headerAuthorization,headerAuthorizationValue).type(headerType).accept("application/json").get(ClientResponse.class);
int statusCode = response.getStatus();
if (statusCode == 401) {
throw new AuthenticationException("401 Invalid Username or Password");
} else if (statusCode == 403) {
throw new AuthenticationException("403 Forbidden");
}
}
}
正如已经写的,我总是收到403错误。 :(如果我在浏览器中输入URL,它可以正常工作,所以我绝对不知所措。有人可以看到我在做什么错吗?非常感谢您的帮助!
编辑2020-05-03
根据PiRocks的建议,我曾经尝试比较通话次数。
对我来说看起来一样,但不幸的是,我不知道标题中的许多细节。我的代码有区别吗?
邮递员: 另外,我尝试向邮递员提出请求。那也行。 授权类型也是“基本身份验证”。
如您所见,我得到了200状态代码,一切正常。我认为该请求与我的代码相同,不是吗?
此外,我尝试了Postman的代码,但奇怪的是,即使使用此代码,我也收到403错误。
其他人有什么想法吗?我用尽了所有的东西来思考可能是什么问题...:(
答案 0 :(得分:0)
我发现了错误。尽管我通过VPN连接到公司网络,但我必须设置代理:
System.getProperties().put("https.proxyHost", proxyHost);
System.getProperties().put("https.proxyPort", proxyPort);
System.getProperties().put("https.proxyUser", proxyUser);
System.getProperties().put("https.proxyPassword", proxyPassword);
System.getProperties().put("https.proxySet", "true");
现在一切正常。这个愚蠢的简单错误使我花费了很多时间...