Java 如何在HTTP代理身份验证中指定目标URL路径
我需要通过代理身份验证将API响应内容从API服务器获取到本地服务器。API请求如下所示:Java 如何在HTTP代理身份验证中指定目标URL路径,java,authentication,proxy,Java,Authentication,Proxy,我需要通过代理身份验证将API响应内容从API服务器获取到本地服务器。API请求如下所示: http://api.example.com/nav_page/head?locale=in&pointer=old&cont=/resque/&scope=old&release=new import org.apache.http.Header; import org.apache.http.HttpEntity; import org.apache.http.
http://api.example.com/nav_page/head?locale=in&pointer=old&cont=/resque/&scope=old&release=new
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
public class ClientProxyAuthentication {
public static void main(String[] args) throws Exception {
DefaultHttpClient httpclient = new DefaultHttpClient();
try {
httpclient.getCredentialsProvider().setCredentials(
new AuthScope("proxy.example.com", 8080),
new UsernamePasswordCredentials("Domain\user", "pass"));
HttpHost targetHost = new HttpHost("api.example.com", 80, "http");
HttpHost proxy = new HttpHost("proxy.example.com", 8080);
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
HttpGet httpget = new HttpGet("/");
System.out.println("executing request: " + httpget.getRequestLine());
System.out.println("via proxy: " + proxy);
System.out.println("to target: " + targetHost);
HttpResponse response = httpclient.execute(targetHost, httpget);
HttpEntity entity = response.getEntity();
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
if (entity != null) {
System.out.println("Response content length: " + entity.getContentLength());
System.out.println(EntityUtils.toString(entity));
}
Header[] headers = response.getAllHeaders();
for (int i = 0; i<headers.length; i++) {
System.out.println(headers[i]);
}
EntityUtils.consume(entity);
} finally {
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpclient.getConnectionManager().shutdown();
}
}
}
我得到一些错误响应,但不是正确的响应
HTTP/1.1 503 Service Unavailable
Response content length: 442
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>503 Service Temporarily Unavailable</title>
</head><body>
<h1>Service Temporarily Unavailable</h1>
<p>The server is temporarily unable to service your
request due to maintenance downtime or capacity
problems. Please try again later.</p>
<p>Additionally, a 404 Not Found
error was encountered while trying to use an ErrorDocument to handle the request.</p>
</body></html>
HTTP/1.1 503服务不可用
回应内容长度:442
503服务暂时不可用
服务暂时不可用
服务器暂时无法为您的服务
由于维护停机时间或容量而提出的请求
问题。请稍后再试
此外,未找到404
尝试使用ErrorDocument处理请求时遇到错误
如果我直接在浏览器中点击URL,我就能得到响应
我可以知道如何发送导航路径请求吗?在上述情况下,如果内容位于目标URL的基本目录中,我将获得它。但由于我对该服务器没有任何控制权,我无法请求将内容移动到我最终发现的基础目录。需要在HttpGet中指定HttpGet=newhttpget(“/nav_page/head?locale=in&pointer=old&cont=/resque/&scope=old&release=new”);参数
HTTP/1.1 503 Service Unavailable
Response content length: 442
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>503 Service Temporarily Unavailable</title>
</head><body>
<h1>Service Temporarily Unavailable</h1>
<p>The server is temporarily unable to service your
request due to maintenance downtime or capacity
problems. Please try again later.</p>
<p>Additionally, a 404 Not Found
error was encountered while trying to use an ErrorDocument to handle the request.</p>
</body></html>