Apache httpcomponent返回java.net.UnknownHostException:名称或服务未知

我试图使用Apache Httpcomponents 4.5.1在Instagram上进行oauthlogin,但是无法成功获取访问令牌。

我很确定这是库本身的问题,因为如果我curl,我会得到我想要的结果。

所以,我尝试了几种不同的方式来完成后调用,但是他们都给了我相同的结果,所以我只是发布我find的最优雅的方式是使用fluent-hc库:

@Value("${instagram.client.id}") private String clientID; @Value("${instagram.client.secret}") private String clientSecret; @Value("${instagram.redirect.uri}") private String redirectURI; private static final String INSTAGRAM_ACCESS_TOKEN_URL = "https://api.instagram.com/oauth/access_token"; private Content requestAccessToken(String code, UserType userType) throws IOException { // String authorization_header_string = URLEncoder.encode(clientID + ":" + clientSecret, "UTF-8"); return Request.Post(INSTAGRAM_ACCESS_TOKEN_URL) .bodyForm(Form.form() .add("client_id", clientID) .add("client_secret", clientSecret) .add("grant_type", "authorization_code") .add("redirect_uri", redirectURI + "?type=" + userType) .add("code", code) .build()) // .addHeader("Authorization", authorization_header_string) .execute().returnContent(); } 

我得到的结果是:

java.net.UnknownHostException:api.instagram.com:名称或服务未知java.net.Inet6AddressImpl.lookupAllHostAddr(Native方法)java.net.InetAddress $ 1.lookupAllHostAddr(InetAddress.java:922)java.net.InetAddress.getAddressesFromNameService (InetAddress.java:1316)java.net.InetAddress.getAllByName0(InetAddress.java:1269)java.net.InetAddress.getAllByName(InetAddress.java:1185)java.net.InetAddress.getAllByName(InetAddress.java:1119)org .apache.http.impl.conn.SystemDefaultDnsResolver.resolve(SystemDefaultDnsResolver.java:45)org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:111)org.apache.http.impl.conn.PoolingHttpClientConnectionManager .connect(PoolingHttpClientConnectionManager.java:353)org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:380)org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236)org .apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java: 184)org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88)org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)org.apache.http.impl。 client.InternaleHttpClient.execute(CloseableHttpClient.java:82)org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:55)org.apache.http.client.fluent.Request.internalExecute(Request.java: 173)org.apache.http.client.fluent.Request.execute(Request.java:177)HttpClient.doExecute(InternalHttpClient.java:184)org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java: 82)org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:55)org.apache.http.client.fluent.Request.internalExecute(Request.java:173)org.apache.http.client。 fluent.Request.execute(Request.java:177)

现在我不知道如何进行。 每一种使用这个lib的请求都会返回这个错误。 试图添加身份validation标题传递密钥:秘密,但也没有工作。

我错过了什么?

PS:我正在使用docker。

好。 这确实与Docker有关。 当我试图运行Docker之外的代码的时候,它工作。

所以,我所做的是:

$ ping api.instagram.com

PING instagram.c10r.facebook.com(173.252.120.81)56(84)字节的数据。

来自instagram-p3-shv-12-frc3.fbcdn.net(173.252.120.81)的64字节:icmp_seq = 1 ttl = 74时间= 146ms

而且因为我使用的是docker-compose,所以我添加到了我的common.ymlconfiguration中:

  extra_hosts: - "api.instagram.com:173.252.120.81" 

现在它工作得很好。

您的代码示例似乎没有使用HttpComponents API。 根据教程中的示例,我生成了这个代码。 它返回一个400响应,因为它实际上是一个不好的请求,但至less它得到了一个响应。

 public class HttpClientExample { private static final String INSTAGRAM_ACCESS_TOKEN_URL = "https://api.instagram.com/oauth/access_token"; public static void main(String[] args) throws IOException { List<NameValuePair> formParams = new ArrayList<NameValuePair>(); formParams.add(new BasicNameValuePair("client_id", "someValue")); formParams.add(new BasicNameValuePair("client_secret", "someValue")); formParams.add(new BasicNameValuePair("grant_type", "someValue")); formParams.add(new BasicNameValuePair("redirect_uri", "someValue")); formParams.add(new BasicNameValuePair("code", "someValue")); UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formParams, Consts.UTF_8); HttpPost post = new HttpPost(INSTAGRAM_ACCESS_TOKEN_URL); post.setEntity(entity); HttpClient client = HttpClients.createDefault(); HttpResponse response = client.execute(post); System.out.println(response.getStatusLine()); System.out.println(response.getEntity()); } }