http网络请求中比较常用的请求方式有get和post请求,其它的如head, put, 以及自定义的OPTIONS请求先忽略
java中使用的请求对象一般是HttpUrlConnection
我们请求的地址需要使用URL对象将字符串地址进行封装
即URL connURL = new URL("http://www.xxxx./")
网络请求的步骤
GET请求
首先要对请求的参数进行中文编码
java.net.URLEncoder.encode(parameters.get(name),
"UTF-8")
否则服务器接收到的中文数据会变成乱码,这是人为的错误。
将编码的参数和url进行拼接形成真正的请求地址
String url = url + "?" + params
1、创建URL对象
URL connURL = new URL("http://www.xxxx./")
2、打开url链接获得HttpUrlConnection
HttpURLConnection httpConn = (HttpURLConnection) connURL
.openConnection();
3、设置连接的属性, ----请求头部信息
也就是模仿浏览器的请求头部的信息
Connection、Accept、User-Agent、Cookie 等其它的请求头header信息
httpConn.setRequestProperty("Accept", "*/*");
4、 建立连接
httpConn.connect();
5、 获取响应头部的信息 ,这里主要是获取到cookie的信息,以便下次访问时不再需要登录,直接通过cookie获取数据
(当然,随着各大网站的特意防御,只获取到cookie,下次携带cookie可能会被拦截,即不能抓取到想要的数据)
httpConn.getHeaderField("keyName");
如果要对response的响应头部的所有信息进行遍历,则代码如下
// 响应头部获取 ???????????Map<String, List<String>> headers = httpConn.getHeaderFields(); ??????????????????????// 遍历所有的响应头字段 ???????????for (String key : headers.keySet()) { ???????????????????????????????System.out.println(key+": ?"+httpConn.getHeaderField(key)); ???????????}
这是模拟访问www.baidu.com返回的response头部信息
6、读取响应的内容,网页html文件,或者特定的返回值数据
// 定义BufferedReader输入流来读取URL的响应,并设置编码方式 BufferedReader in= null; ???????????in = new BufferedReader(new InputStreamReader(httpConn ???????????????????.getInputStream(), "UTF-8"));//通用编码格式为utf-8 ???????????String line; ???????????// 读取返回的内容 ???????????while ((line = in.readLine()) != null) { ???????????????result += line; ???????????} ???????????httpConn.disconnect();//最后需要关闭httpConn连接
还有需要关闭,字符输入流BufferedReader
这个需要在finally块中进行关闭
finally{ ???try { ????????if (in != null) { ???????????in.close(); ????????} ????} catch (IOException ex) { ??????????ex.printStackTrace(); ?????}}
上面是一个get请求的正常流程,当然其中的读取内容的方式有很多种,有些可以直接保存在电脑的文件中,这些方式先不考虑
一个get请求需要注意的几点:
- 请求参数,中文,其它的转义符号需要进行编码
如下代码,中文和特殊意义字符都会被进行编码,而英语字母则不需要编码
String url = "http://www.baidu.com"; ???????String ss="你是逗逼吗"; ???????try { ???????????System.out.println(java.net.URLEncoder.encode(url,"UTF-8")); ???????????System.out.println(java.net.URLEncoder.encode(ss,"UTF-8")); ???????} catch (UnsupportedEncodingException e) { ???????????e.printStackTrace(); ???????}
输出结果:
- get请求头部最好设置个代理,否则有可能被网站给拦截,拒接访问
,如这里设置为火狐浏览器,模拟浏览器的请求头即可
httpConn.setRequestProperty("User-Agent", ???????????????????"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
- 连接断开时候记得一定要把输入输出流给关掉,降低异常出现
如上面的HttpUrlConnection连接,还有bufferReader对象
一个完整的get请求代码
???public static String sendGet(String url, LinkedHashMap<String, String> parameters) { ???????String result = "";// 返回的结果 ???????BufferedReader in = null;// 读取响应输入流 ???????StringBuffer sb = new StringBuffer();// 存储参数 ???????String params = "";// 编码之后的参数 ???????try { ???????????// 编码请求参数 ???????????if (parameters.size() == 1) { ???????????????for (String name : parameters.keySet()) { ???????????????????sb.append(name).append("=").append( ???????????????????????????java.net.URLEncoder.encode(parameters.get(name), ???????????????????????????????????"UTF-8")); ???????????????} ???????????????params = sb.toString(); ???????????} else { ???????????????for (String name : parameters.keySet()) { ???????????????????sb.append(name).append("=").append( ???????????????????????????java.net.URLEncoder.encode(parameters.get(name), ???????????????????????????????????"UTF-8")).append("&"); ???????????????} ???????????????String temp_params = sb.toString(); ???????????????params = temp_params.substring(0, temp_params.length() - 1); ???????????} ???????????String full_url = url + "?" + params; ???????????// 创建URL对象 ???????????URL connURL = new URL(full_url); ???????????// 打开URL连接 ???????????HttpURLConnection httpConn = (HttpURLConnection) connURL ???????????????????.openConnection(); ???????????// 设置通用属性 ???????????httpConn.setRequestProperty("Accept", "*/*"); ???????????httpConn.setRequestProperty("Connection", "Keep-Alive"); ???????????httpConn.setRequestProperty("User-Agent", ???????????????????"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)"); ???????????// 建立实际的连接 ???????????httpConn.connect(); ???????????// 响应头部获取 ???????????Map<String, List<String>> headers = httpConn.getHeaderFields(); ???????????????????????// 遍历所有的响应头字段 ???????????for (String key : headers.keySet()) { ???????????????//System.out.println(key+": ?"+httpConn.getHeaderField(key)); ???????????} ???????????// 定义BufferedReader输入流来读取URL的响应,并设置编码方式 ???????????in = new BufferedReader(new InputStreamReader(httpConn ???????????????????.getInputStream(), "GBK")); ???????????String line; ???????????// 读取返回的内容 ???????????while ((line = in.readLine()) != null) { ???????????????result += line; ???????????} ???????} catch (Exception e) { ???????????e.printStackTrace(); ???????????System.out.println("Http请求方法内部问题"); ???????} finally { ???????????try { ???????????????if (in != null) { ???????????????????in.close(); ???????????????} ???????????} catch (IOException ex) { ???????????????ex.printStackTrace(); ???????????} ???????} ???????return result; ???}
POST网络请求方式
post请求跟get请求的方式基本差不多,区别在于传递请求数据的方式不一样,post可以提交的数据大小没有限制,
而get请求理论上没有大小限制,由于浏览器限制其长度,所以传递数据大小有限制
post请求不需要设置请求投的user-agent
如果要避免登陆,则需要将登陆时返回的请求头中cookie信息在httpUrlConnect中设置请求投信息即可
如 connection.setRequestProperty("Cookie", "这里放cookie数据");
connection.setRequestProperty("Content-Type", ???????????????????"application/x-www-form-urlencoded;charset=utf-8");
主要进行设置服务器解析数据的方式,服务器会将这个将body里面的数据以键值对key value方式进行解析,
我们在后台只需要用request.getParameter("keyName")就可以将数据读取出来
其它提交数据的格式:
- multipart/form-data类型主要是上传文件时用到;
- application/x-www-form-urlencoded类型主要是提交k-v时用到,当然这种方法也可以将json设置在v中提交json数据;
- application/json类型主要是传递json数据用到,层次比较深的数据;
public static String sendPost(String curl, String param) { ???????String result = "";// 返回的结果 ???????BufferedReader in = null;// 读取响应输入流 ???????try { ???????????//创建连接 ???????????URL url = new URL(curl); ???????????HttpURLConnection connection = (HttpURLConnection) url ???????????????????.openConnection(); ???????????connection.setDoOutput(true); ???????????connection.setDoInput(true); ???????????connection.setRequestMethod("POST"); ???????????connection.setUseCaches(false); ???????????connection.setInstanceFollowRedirects(true); ???????????connection.setRequestProperty("Content-Type", ???????????????????"application/x-www-form-urlencoded;charset=utf-8"); ???????????connection.connect(); ???????????//POST请求 ???????????BufferedWriter out = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream(),"UTF-8")); ???????????out.write(param); ???????????out.flush(); ???????????out.close(); ???????????//读取响应 ???????????// 定义BufferedReader输入流来读取URL的响应,并设置编码方式 ???????????in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8")); ???????????String line; ???????????// 读取返回的内容 ???????????while ((line = in.readLine()) != null) { ???????????????result += line; ???????????} ???????} catch (Exception e) { ???????????e.printStackTrace(); ???????????System.out.println("Http请求方法内部问题"); ???????} finally { ???????????try { ???????????????if (in != null) { ???????????????????in.close(); ???????????????} ???????????} catch (IOException ex) { ???????????????ex.printStackTrace(); ???????????} ???????} ???????return result; ???}
post请求不同提交数据方式有对应的解析方法,json解析和文件上传下次再写个专题
http网络请求基础get,post请求的注意事项
原文地址:https://www.cnblogs.com/gne-hwz/p/9403786.html