Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
&引用;407需要代理身份验证";java rest请求期间偶尔出错_Java_Spring_Rest_Web Services_Jakarta Ee - Fatal编程技术网

&引用;407需要代理身份验证";java rest请求期间偶尔出错

&引用;407需要代理身份验证";java rest请求期间偶尔出错,java,spring,rest,web-services,jakarta-ee,Java,Spring,Rest,Web Services,Jakarta Ee,我正在尝试对我的一个客户端URL执行REST请求,以使用代理获取响应。大多数情况下,我可以使用我的代码获取响应。但有时当我尝试使用我的代码发送请求时,我会收到“407 proxy Authentication Required”(需要代理身份验证)错误。这种情况很少发生,但一旦出现此错误,对于每个连续的请求,我都会收到相同的错误。但是当我使用chrome的POSTMAN工具将生成的相同请求发送到相同的URL时,我会得到回复。但是,当我再次尝试使用我的代码时,一旦收到邮递员的回复,我不仅会从我的本

我正在尝试对我的一个客户端URL执行REST请求,以使用代理获取响应。大多数情况下,我可以使用我的代码获取响应。但有时当我尝试使用我的代码发送请求时,我会收到“407 proxy Authentication Required”(需要代理身份验证)错误。这种情况很少发生,但一旦出现此错误,对于每个连续的请求,我都会收到相同的错误。但是当我使用chrome的POSTMAN工具将生成的相同请求发送到相同的URL时,我会得到回复。但是,当我再次尝试使用我的代码时,一旦收到邮递员的回复,我不仅会从我的本地机器上得到响应,还会从运行代码的不同机器上得到响应。我对这个问题非常困惑,无法理解为什么会出现这种奇怪的情况。我有什么不对劲吗我的代码缺失。请帮帮我,伙计们。我已经给出了下面的代码:

代码
publicmap-ConnectRestService(MyRequest-MyRequest、String-postrl、String-httpProxy、int-timeout-int-httpPort、Map-responseMap)
抛出KeyManagementException、UnrecoverableKeyException、NoSuchAlgorithmException、KeyStoreException、CustomException{
MyResponse MyResponse=新的MyResponse();
Map responseReturnMap=新HashMap();
字符串输出=”;
TrustStrategy acceptingTrustStrategy=新信任策略(){
@凌驾
已信任公共布尔值(java.security.cert.X509Certificate[]链,字符串authType)
抛出java.security.cert.CertificateException{
//TODO自动生成的方法存根
返回true;
}
};
SSLContext SSLContext=SSLContexts.custom()
.loadTrustMaterial(空,acceptingTrustStrategy)
.build();
SSLConnectionSocketFactory csf=新的SSLConnectionSocketFactory(sslContext);
HttpClient-HttpClient;
httpClient=HttpClients.custom().setSSLSocketFactory(csf).setProxy(新的HttpHost(httpProxy,httpPort)).build();
HttpHeaders HttpHeaders=新的HttpHeaders();
添加(“内容类型”、“文本/xml”);
添加(“接受”、“文本/xml”);
httpHeaders.add(“访问控制允许来源”、“*”);
添加(“内容编码”、“UTF-8”);
HttpComponents客户端HttpRequestFactory requestFactory=新的HttpComponents客户端HttpRequestFactory(httpClient);
setConnectTimeout(超时);
试一试{
RestTemplate RestTemplate=新的RestTemplate(requestFactory);
HttpEntity entity=new org.springframework.http.HttpEntity(
myRequest,httpHeaders);
ResponseEntity response=restemplate.exchange(postrl,HttpMethod.POST,entity,String.class);
}捕获(例外情况除外){
logger.error(例如getMessage(),例如);
}
返回响应返回图;
}

请帮我找出我在这里遗漏了什么。

根据我的观察,在这种情况下,一些软件只是显示一个弹出窗口,要求提供凭据。很多时候,此窗口“落后于”您正在工作的当前窗口,您甚至从未注意到有人要求提供凭据。因此,在许多情况下,作为一个人,您甚至没有输入凭证,因为(不幸的是)弹出窗口显示在当前工作窗口的“后面”

所以,下次遇到这样的问题时,请快速检查是否有任何本机弹出窗口出现在幕后,并在那里快速输入您的凭据

现在,我看到的另一件事是,当再次连接到URL时,某些命令行工具(例如git等)必须通过代理,这些工具有时在凭证管理方面做得很糟糕。为此,还有一种方法是Windows中提供的“凭据管理器”。只需转到那里,在凭证管理器中“添加”URL并在那里输入凭证,然后关闭所有命令行窗口并重试您尝试的操作。这一次一切都会好起来的

凭证有“味道”,一种带有“域名”(通常是在你有公司代理的情况下)

示例:您在公司网络中的Windows登录ID可以是'Deepak',也可以是DOMAINNAME\Deepak,因此请在'Windows凭据管理器'中以用户名的形式尝试这两个ID

试试这些,一切都好


如果出现
407
错误,您收到的响应必须包含一个特殊的
代理验证
头。此标头将告诉您代理服务器所需的身份验证类型

您需要做的是在请求中包含
代理授权
头。代理授权头的典型语法是
代理授权:


代理身份验证
标题将让您知道需要使用的身份验证方案的类型

看着你的代码,我开始对这行代码感兴趣:

httpHeaders.add(“访问控制允许来源”,“*”)

不使用星号,您可以尝试指定源的来源,即请求发出的位置。当chrome或firefox等浏览器发现源代码丢失时,可能是由于域不同或CORS问题,它们的行为可能并不总是正确的

当请求被发送时,网络层试图查找资源,而这里的非简单头似乎引起了问题或混淆了浏览器。 浏览器首先发送一个无数据选项请求[查看要提供的服务的请求的基本第一个操作,如果发现为真,则继续实际请求],以确认服务器将接受适当响应的请求

我认为内容类型不是以下任何一种类型,比如application/x-www-form-urlencoded、multipart/formdata或text/plain,这使得请求变得不简单。这里有更多关于这个的信息

此外,最好检查您的java程序是否为sendi
public Map<String ,Object> ConnectRestService(MyRequest myRequest, String postURL, String httpProxy,int timeout int httpPort, Map<String ,Object> responseMap)
        throws KeyManagementException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyStoreException, CustomException{

    MyResponse myResponse = new MyResponse();
    Map<String ,Object> responseReturnMap = new HashMap<>();
    String output = "";
    TrustStrategy acceptingTrustStrategy = new TrustStrategy() {

        @Override
        public boolean isTrusted(java.security.cert.X509Certificate[] chain, String authType)
                throws java.security.cert.CertificateException {
            // TODO Auto-generated method stub
            return true;
        }
    };

    SSLContext sslContext = SSLContexts.custom()
            .loadTrustMaterial(null, acceptingTrustStrategy)
            .build();


    SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext);
    HttpClient httpClient;
    httpClient = HttpClients.custom().setSSLSocketFactory(csf).setProxy(new HttpHost(httpProxy, httpPort)).build();

    HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.add("Content-type", "text/xml");
    httpHeaders.add("Accept", "text/xml");
    httpHeaders.add("access-control-allow-origin", "*");
    httpHeaders.add("content-encoding", "UTF-8");
    HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
    requestFactory.setConnectTimeout(timeout);


    try {
        RestTemplate restTemplate = new RestTemplate(requestFactory);
        HttpEntity<MyRequest> entity = new org.springframework.http.HttpEntity<MyRequest>(
                myRequest, httpHeaders);
        ResponseEntity<String> response = restTemplate.exchange(postURL, HttpMethod.POST, entity, String.class);


    } catch (Exception ex) {
        logger.error(ex.getMessage(), ex);
    }

    return responseReturnMap;
}
 private final RestTemplate getProxiedRestTemplate() {
    RestTemplate r = null;
    if (isProxyActivated()) {
        HttpClientBuilder client = HttpClientBuilder.create();
        HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
        client.setProxy(new HttpHost("your ip", 8080 //or the port you want to use}
));
        if (withAuthentication()) {
            Credentials credentials = new UsernamePasswordCredentials(getProxyUsername(), getProxyPassword());
            AuthScope authScope = new AuthScope("your ip", 8080);
            CredentialsProvider credsProvider = new BasicCredentialsProvider();
            credsProvider.setCredentials(authScope, credentials);
            client.setDefaultCredentialsProvider(credsProvider);
        }
        requestFactory.setHttpClient(client.build());
        r = new RestTemplate(requestFactory);
    } else {
        r = new RestTemplate();
    }
    return r;
}
String data = "user:password"; // Here proxy user and password to be used.
String encodedValue = Base64.getEncoder().encodeToString(data.getBytes());
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("Proxy-Authorization", "Basic " + encodedValue); // You need to add proxy-authorization in the header.
ss -s