在向https://www.googleapis.com/发送请求时获取java.net.UnknownHostException

时间:2017-09-26 18:22:38

标签: java google-api weblogic11g google-url-shortener

我有以下使用谷歌URL缩短服务的代码。代码运行得很好但是当使用代理运行它会返回一个未知的主机异常有没有办法通过我的代码点击https://www.ietf.org/rfc/rfc4229.txt#2.1.30进行身份验证。

我使用了以下代码:

String json = "{\"longUrl\": \""+longUrl+"\"}";   
String apiURL = GOOGLE_URL_SHORT_API+"?key="+GOOGLE_API_KEY;


System.setProperty("proxySet", "true");
System.setProperty("http.proxyHost", proxyHost);
System.setProperty("http.proxyPort", proxyPort);    

AuthCacheValue.setAuthCache(new AuthCacheImpl());
Authenticator.setDefault(new Authenticator() 
{
    protected PasswordAuthentication getPasswordAuthentication() 
    {
    return new PasswordAuthentication(proxyUser,proxyPassword.toCharArray());
    }
});


HttpPost postRequest = new HttpPost(apiURL);
postRequest.setHeader("Content-Type", "application/json");
postRequest.setEntity(new StringEntity(json, "UTF-8"));

CloseableHttpClient httpClient = HttpClients.createDefault();
HttpResponse response = httpClient.execute(postRequest);
String responseText = EntityUtils.toString(response.getEntity());           

Gson gson = new Gson();
@SuppressWarnings("unchecked")
HashMap<String, String> res = gson.fromJson(responseText, HashMap.class);

需要建议。

0 个答案:

没有答案