Created
September 20, 2014 08:09
-
-
Save precious-ming/bd18ec094b46f2a2c8f6 to your computer and use it in GitHub Desktop.
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")); | |
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