Commit e7c4e436 authored by jim's avatar jim

tran

parent d8296e81
package com.edgec.browserbackend.common.utils; package com.edgec.browserbackend.common.utils;
import org.apache.commons.io.Charsets;
import org.apache.http.HttpEntity; import org.apache.http.HttpEntity;
import org.apache.http.HttpHost; import org.apache.http.HttpHost;
import org.apache.http.HttpResponse; import org.apache.http.HttpResponse;
...@@ -12,12 +13,11 @@ import org.apache.http.client.methods.HttpGet; ...@@ -12,12 +13,11 @@ import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicCredentialsProvider; import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader;
public class Trans { public class Trans {
private final Logger logger = LoggerFactory.getLogger(Trans.class); private final Logger logger = LoggerFactory.getLogger(Trans.class);
...@@ -50,7 +50,7 @@ public class Trans { ...@@ -50,7 +50,7 @@ public class Trans {
*/ */
public static void main(String[] args) { public static void main(String[] args) {
String url = "http://free.ipwhois.io/json/"; String url = "http://free.ipwhois.io/json/";
Trans trans = new Trans("23.105.2.118", "uE5LkzM81UhUaA1"); Trans trans = new Trans("112.74.47.217", 20001, "fangguanlianbrowser", "5D753F36QKqIaA1");
String rs = trans.get(url); String rs = trans.get(url);
System.out.println(rs); System.out.println(rs);
} }
...@@ -84,17 +84,7 @@ public class Trans { ...@@ -84,17 +84,7 @@ public class Trans {
HttpResponse response = client.execute(httpGet); HttpResponse response = client.execute(httpGet);
HttpEntity entry = response.getEntity(); HttpEntity entry = response.getEntity();
return EntityUtils.toString(entry, Charsets.UTF_8);
if (entry != null) {
InputStreamReader is = new InputStreamReader(entry.getContent());
BufferedReader br = new BufferedReader(is);
String str = null;
while ((str = br.readLine()) != null) {
sb.append(str.trim());
}
br.close();
}
return sb.toString();
} catch (ClientProtocolException e) { } catch (ClientProtocolException e) {
logger.error(e.getMessage(), e); logger.error(e.getMessage(), e);
return ""; return "";
...@@ -106,6 +96,11 @@ public class Trans { ...@@ -106,6 +96,11 @@ public class Trans {
return ""; return "";
} finally { } finally {
httpGet.releaseConnection(); httpGet.releaseConnection();
try {
client.close();
} catch (IOException e) {
logger.error("fail to close connection", e);
}
} }
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment