//This is my code for returning json response from twitter api which works fine when i am not behind any proxy server
import oauth.signpost.OAuthConsumer;
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
public class TwitterFeeds {
static String AccessToken = "xxxx";
static String AccessSecret = "xxxx";
static String ConsumerKey = "xxxx";
static String ConsumerSecret = "xxxx";
public static void main(String[] args) throws Exception {
OAuthConsumer consumer = new CommonsHttpOAuthConsumer(ConsumerKey, ConsumerSecret);
consumer.setTokenWithSecret(AccessToken, AccessSecret);
HttpGet request = new HttpGet("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=xxxx&count=2");
consumer.sign(request);
HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(request);
String json_string = EntityUtils.toString(response.getEntity());
System.out.println(json_string);
JSONArray obj = new JSONArray (json_string);
System.out.println(obj.toString());
}
}
//我在阻止我的请求的代理服务器后面,当我不在任何代理服务器后面时,代码工作正常。并且收到以下错误
Exception in thread "main" org.apache.http.conn.HttpHostConnectException: Connection to https://api.twitter.com refused
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:158)
at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:149)
at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:121)
at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:562)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:415)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:820)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:754)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:732)
at TwitterFeeds.main(TwitterFeeds.java:47)
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 org.apache.http.conn.ssl.SSLSocketFactory.connectSocket(SSLSocketFactory.java:374)
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:148)
... 8 more
这是我显示连接被拒绝的错误。以及用户获取推文(json格式)的代码,当我不在任何代理服务器后面时工作,当我在代理服务器后面时不起作用
答案 0 :(得分:1)
我认为您的HttpGet
请求缺少代理设置。
您应该执行以下操作:
// Here you set your proxy host and port
HttpHost proxy = new HttpHost(<your_proxy_addess>, <port>);
DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
HttpClient client = HttpClients.custom().setRoutePlanner(routePlanner).build();
HttpResponse response = client.execute(request);
如果您认为提供任何类型的代理凭证是必要的,那么应采取以下方法:
创建HttpGet
请求
HttpHost proxy = new HttpHost(<your_proxy_addess>, <port>);
RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
HttpGet request = new HttpGet("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=xxxx&count=2");
request.setConfig(config);
将您的凭据提供给HttpClient
:
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope(<your_proxy_addess>, <port>),
new UsernamePasswordCredentials("user", "passwd"));
HttpClient client = HttpClients.custom()
.setDefaultCredentialsProvider(credsProvider).build();
HttpResponse response = client.execute(request);
答案 1 :(得分:0)
我认为你因代理错误而走上正轨。
在java中,至少有2-3种方法来绕过&#34;绕过&#34;代理人。他们每个人都有优点和不便:
$ java -Dhttp.proxyHost=webcache.example.com -Dhttp.proxyPort=8080 -Dhttp.proxyUser=someUserName -Dhttp.proxyPassword=somePassword
通过这样做,所有HTTP连接都受此设置的影响。按照下一点,你可以采用一种稍微动态的方法。
与上面的方法一样,您可以为JVM设置和取消设置属性,但现在可以直接在代码中设置和取消设置。
//Set the http proxy to webcache.example.com:8080
System.setProperty("http.proxyHost", "webcache.example.com");
System.setProperty("http.proxyPort", "8080");
// Now, let's 'unset' the proxy.
System.setProperty("http.proxyHost", null);
如果您希望保持一切简单并且您的应用程序不是多线程,那么这是一个很好的解决方案。就像我之前说的那样,为所有JVM设置了属性。
这个解决方案比另外两个解决方案更灵活。
SocketAddress addr = new InetSocketAddress("webcache.example.com", 8080);
Proxy proxy = new Proxy(Proxy.Type.HTTP, addr);
URL url = new URL("http://java.example.org/");
URConnection conn = url.openConnection(proxy);
注意:对于HTTPS,只需使用Proxy.Type.HTTPS
即可。对于其他方法,请使用https.proxyPort
代替http.proxyPort
(例如)