标签:style exce post gen 4.0 clip 限制 区别 读取
// 响应头部获取 Map<String, List<String>> headers = httpConn.getHeaderFields(); // 遍历所有的响应头字段 for (String key : headers.keySet()) { System.out.println(key+": "+httpConn.getHeaderField(key)); }
这是模拟访问www.baidu.com返回的response头部信息
// 定义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连接
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(); }
输出结果:
,如这里设置为火狐浏览器,模拟浏览器的请求头即可
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; }
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded;charset=utf-8");
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解析和文件上传下次再写个专题
标签:style exce post gen 4.0 clip 限制 区别 读取
原文地址:https://www.cnblogs.com/gne-hwz/p/9403786.html