我一直在尝试使用Java连接Bitly REST API。我想使用Bitly API中的缩短方法。这是我的代码:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.log4j.BasicConfigurator;
public class PostRequestClient {
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
BasicConfigurator.configure();
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost(
"https://api-ssl.bitly.com/v3/shorten");
StringEntity input = new StringEntity("{\"access_token\":<my_access_token>,\"longUrl\":\"https://docs.appian.com/suite/help/18.1/Custom_Function_Plug-ins.html\"}");
input.setContentType("application/json");
postRequest.setEntity(input);
HttpResponse response = httpClient.execute(postRequest);
if (response.getStatusLine().getStatusCode() != 201) {
throw new RuntimeException("Failed : HTTP error code : "
+ response.getStatusLine().getStatusCode());
}
BufferedReader br = new BufferedReader(
new InputStreamReader((response.getEntity().getContent())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}
httpClient.getConnectionManager().shutdown();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
我已从https://www.mkyong.com/webservices/jax-rs/restful-java-client-with-apache-httpclient/引用此代码。请告诉我你是否正确地调用了这个有点api的想法。 我收到了以下错误:
0 [main] DEBUG org.apache.http.impl.conn.SingleClientConnManager - Get connection for route HttpRoute[{s}->https://api-ssl.bitly.com]
31 [main] DEBUG org.apache.http.impl.conn.DefaultClientConnectionOperator - Connecting to api-ssl.bitly.com/XX.XXX.XXX.21:443
21059 [main] DEBUG org.apache.http.impl.conn.DefaultClientConnectionOperator - Connect to api-ssl.bitly.com/XX.XXX.XXX.21:443 timed out. Connection will be retried using another IP address
21059 [main] DEBUG org.apache.http.impl.conn.DefaultClientConnectionOperator - Connecting to api-ssl.bitly.com/XX.XXX.XXX.20:443
42080 [main] DEBUG org.apache.http.impl.conn.DefaultClientConnection - Connection closed
org.apache.http.conn.HttpHostConnectException: Connection to https://api-ssl.bitly.com refused
42080 [main] DEBUG org.apache.http.impl.conn.DefaultClientConnection - Connection shut down
42080 [main] DEBUG org.apache.http.impl.conn.SingleClientConnManager - Releasing connection org.apache.http.impl.conn.SingleClientConnManager$ConnAdapter@7225790e
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 com.persistent.plugins.urlshortener.PostRequestClient.main(PostRequestClient.java:70)
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
如果你能给我一些输入来解决这个错误,我将不胜感激。
谢谢!