-
-
Save wdfx100/5566099 to your computer and use it in GitHub Desktop.
#第三方api组件的应用#apache HttpComponents HTTP client的使用
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
HttpClient client = null; | |
try { | |
client = new DefaultHttpClient();//获得一个代理 | |
HttpGet get = new HttpGet("http://www.youdao.com/smartresult-xml/search.s?type=id&q="+code);//获得请求 | |
HttpResponse response = client.execute(get);//获得响应 | |
InputStream stream = response.getEntity().getContent(); //从内存角度,获得一个输入流 | |
BufferedReader reader = new BufferedReader(new InputStreamReader(stream,"GBK")); | |
//获得的文字,以字符流的形式输出【图片、MP3,文件用字节流输出】 | |
StringBuilder sb = new StringBuilder(); | |
String str = null; | |
while((str = reader.readLine()) != null) { | |
sb.append(str); | |
} | |
reader.close(); | |
stream.close(); | |
return sb.toString(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} finally { | |
if(client != null) | |
client.getConnectionManager().shutdown();//关闭连接 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment