Java 在Http Rest客户端中使用代理时发生异常
您好,我正在使用Http Rest客户端登录SalesForce Oauth身份验证。。。。 当我没有代理时,程序运行良好…但是使用Https和代理设置对我不起作用…我的代码是 公营帽匠{Java 在Http Rest客户端中使用代理时发生异常,java,rest,https,proxy,salesforce,Java,Rest,Https,Proxy,Salesforce,您好,我正在使用Http Rest客户端登录SalesForce Oauth身份验证。。。。 当我没有代理时,程序运行良好…但是使用Https和代理设置对我不起作用…我的代码是 公营帽匠{ public static void main(String args[]) throws ParseException, HttpException, IOException, SOAPException, NoSuchAlgorithmException { System.out
public static void main(String args[]) throws ParseException,
HttpException, IOException, SOAPException, NoSuchAlgorithmException {
System.out.println("started");
try {
System.getProperties().put("http.proxyHost", "proxy.company.com");
System.getProperties().put("http.proxyPort", "80");
System.getProperties().put("http.proxyUser", "abcde");
System.getProperties().put("http.proxyPassword", "********");
SSLContext sslContext = SSLContext.getInstance("SSL");
// set up a TrustManager that trusts everything
sslContext.init(null, new TrustManager[] { new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
System.out.println("getAcceptedIssuers =============");
return null;
}
public void checkClientTrusted(X509Certificate[] certs,
String authType) {
System.out.println("checkClientTrusted =============");
}
public void checkServerTrusted(X509Certificate[] certs,
String authType) {
System.out.println("checkServerTrusted =============");
}
} }, new SecureRandom());
SSLSocketFactory sf = new SSLSocketFactory(sslContext);
Scheme httpsScheme = new Scheme("https", 443, sf);
SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(httpsScheme);
// apache HttpClient version >4.2 should use
// BasicClientConnectionManager
ClientConnectionManager cm = new BasicClientConnectionManager(
schemeRegistry);
DefaultHttpClient httpClient = new DefaultHttpClient(cm);
HttpPost postRequest = new HttpPost(
"https://login.salesforce.com/services/oauth2/token");
String username = "------------";
String password = "---------------";
String client_id = "3MVGS";
String client_secret = "894448";
List<BasicNameValuePair> parametersBody = new ArrayList<BasicNameValuePair>();
parametersBody
.add(new BasicNameValuePair("grant_type", "password"));
parametersBody.add(new BasicNameValuePair("username", username));
parametersBody.add(new BasicNameValuePair("password", password));
parametersBody.add(new BasicNameValuePair("client_id", client_id));
parametersBody.add(new BasicNameValuePair("client_secret",
client_secret));
postRequest.setEntity(new UrlEncodedFormEntity(parametersBody));
postRequest.setHeader("Content-Type",
"application/x-www-form-urlencoded");
HttpResponse response = httpClient.execute(postRequest);
BufferedReader br = new BufferedReader(new InputStreamReader(
(response.getEntity().getContent())));
JSONParser parser = new JSONParser();
Object obj = parser.parse(br.readLine());
JSONObject jsonObject = (JSONObject) obj;
System.out.println(jsonObject.toString());
String id = (String) jsonObject.get("id");
System.out.println("ID: " + id);
String access_token = (String) jsonObject.get("access_token");
System.out.println("ACCESS_TOKEN: " + access_token);
httpClient.getConnectionManager().shutdown();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (org.json.simple.parser.ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
这甚至可能是由于互联网速度缓慢造成的connection@Nabin有时我确实会收到连接超时错误,这是由于internet连接速度较慢。我收到连接被拒绝的错误。连接被拒绝是否也是由于相同的原因?可能是由于服务器停机。@Nabin认为它与服务器无关,因为我已经尝试了好几天了s now和Salesforce服务器可能会宕机这么长时间。我觉得这是代理的问题。api只支持HTTPS调用。您能确认我在HTTP代码中所做的代理设置也将应用于HTTPS吗?如果不能,如何为HTTPS指定代理?
org.apache.http.conn.HttpHostConnectException: Connection to https://login.salesforce.com refused
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:190)
at org.apache.http.impl.conn.ManagedClientConnectionImpl.open(ManagedClientConnectionImpl.java:294)
at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:640)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:479)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:906)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:805)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:784)
at com.chatter.sf.SfChatter.main(SfChatter.java:100)
Caused by: java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.security.ssl.SSLSocketImpl.connect(Unknown Source)
at org.apache.http.conn.ssl.SSLSocketFactory.connectSocket(SSLSocketFactory.java:549)
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:180)
... 7 more