一.JDK自带的API
- 1.创建URL对象
- URL url = new URL(地址)
- 2创建HttpURLConnection对象
- HttpURLConnection httpconectioon = (HttpURLConnection)url.openConnection()
- 3.进行设置
- conn.setConnectTimeout(3000);
- conn.setDoInput(true);
- conn.setRequestMethod("GET");
- 4.连接并获得响应数据
- int code = conn.getResponseCode();
- if(code == 200){
- }
复制代码
二.Apache工具类
- 1.生成代表客户端的HttpClient对象
- HttpClient httpClient = new DefaultHttpClient( );
- 2.生成代表请求的HttpGet对象
- HttpGet httpGet = new httpGet("网址");
- 3.发送请求获取服务器返回的HttpResponse对象
- HttpResponse resp = httpClient.execute(httpGet);
- 4.检查响应状态是否正常
- int code = resp.getStatusline( ).getStatusCode( );
- if(code == 200){正常}
- 5.获取响应对象当中的数据
- HttpEntity entity = resp.getEntity( );
- InputStram in = entity.getContent;
- 1. 生成代表客户端的HttpClient对象
- HttpClient httpClient = new DefaultHttpClient( ) ;
- 2. 生成代表请求的HttPostt对象
- HttpPost httpPost = new httpPost("网址");
- 3.将要发送的数据放在键值对对象中
- 4.将键值对对象放在List中
- 5.将List放在请求体中
- 6.将请求体放在请求对象中
- 7.发送请求获取服务器返回的HttpResponse对象
- HttpResponse resp = httpClient.execute(httpPost);
- 8.检查响应状态是否正常
- int code = resp.getStatusline( ).getStatusCode( );
复制代码
|
|