HttpClient---------demo
4376 ワード
public class aa {
public static void main(String[] args) {
// HttpClient
HttpClient httpclient = new DefaultHttpClient();
// Get
HttpPost httpPost = new HttpPost(
"http://localhost:8080/sso/modify/modify.action");
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
//
nvps.add(new BasicNameValuePair("data", "wwwq"));
// UTF-8
try {
httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
HttpResponse response = httpclient.execute(httpPost);
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instreams = entity.getContent();
String str = convertStreamToString(instreams);
System.out.println("Do something");
System.out.println(str);
// Do not need the rest
}
} catch (Exception e) {
e.printStackTrace();
}finally{
httpPost.abort();
}
}
public static String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "
");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
}