java简单仿winhttp 发送get post请求
来源:互联网 发布:阿里云 vs aws 编辑:程序博客网 时间:2024/06/07 03:05
package java_http;import java.io.*;import java.net.URL;import java.net.URLConnection;import java.util.ArrayList;import java.util.List;import java.util.Map;/** * Created by 淡漠Vip * Email: itdreamlmc@163.com */public class JavaHttp { private String f_cookies = ""; private String f_xieyitou = ""; private String Status = ""; public String getF_cookies() { return f_cookies; } public String getF_xieyitou() { return f_xieyitou; } public String getStatus() { return Status; } /*** * 去除数组中重复的记录 * @param a * @return */ public String[] array_unique(String[] a) { // array_unique java.util.List list = new ArrayList(); for (int i = 0; i < a.length; i++) { if (!list.contains(a[i])) { list.add(a[i]); } } return (String[]) list.toArray(new String[list.size()]); } private byte[] byteMerger(byte[] byte_1, byte[] byte_2) { byte[] byte_3 = new byte[byte_1.length + byte_2.length]; System.arraycopy(byte_1, 0, byte_3, 0, byte_1.length); System.arraycopy(byte_2, 0, byte_3, byte_1.length, byte_2.length); return byte_3; } private byte[] GetMeass(InputStream in) throws IOException { byte[] by = new byte[1024]; byte[] reault = null; while (in.read(by) > 0) { reault = byteMerger(reault, by); } return reault; } /*** * 获取协议头名 * @param data * @return */ private String getXYName(String data) { int Sy = data.indexOf(":"); String realt = data.substring(0, Sy); if (realt.isEmpty()) realt = null; else { realt = realt.trim(); } // System.out.print("协议数据:" + data + "\n截取协议头Name:" + realt + "\n"); return realt; } /*** * 获取协议头值 * @param data * @return */ private String getXYValu(String data) { int Sy = data.indexOf(":"); String realt = data.substring(Sy + 1, data.length()); if (realt.isEmpty()) realt = null; else { realt = realt.trim(); } // System.out.print("协议数据:" + data + "\n截取协议头Valu:" + realt + "\n"); return realt; } /** * 合并更新Cookies * * @param oldcks * @param newcks * @return */ public String merge_Cookies(String oldcks, String newcks) { // 合并更新Cookies String newcookies = null, Name = null, Value = null; String[] odeck = null, newck = null; int sy = 0; java.util.List Z = new ArrayList(); boolean biaozhi = false; odeck = oldcks.split(";"); newck = newcks.split(";"); for (int i = 0; i < newck.length; i++) { Name = getCookiesName(newck[i]); Value = getCookiesValue(newck[i]); for (int j = 0; j < odeck.length; j++) { if (odeck[j].contains(Name)) { odeck[j] = Name + "=" + Value; biaozhi = true; break; } } if (biaozhi == false) { Z.add(Name + "=" + Value + ";"); } } for (int i = 0; i < odeck.length; i++) { newcookies += odeck[i] + "; "; } String a = null; a = Z.toString().replace("[", ""); a = a.replace("]", ""); a = a.replace(",", ""); a = a.replace("null", ""); newcookies += a; return newcookies; } /** * 获取Cookies名 * * @param data * @return */ private String getCookiesName(String data) { String Name = null; int sy = 0; data = data.replace(",", ""); data = data.trim(); if (data.contains("=")) { sy = data.indexOf("="); Name = data.substring(0, sy); } else { Name = ""; } Name = Name.trim(); // System.out.println("取Cookies名:" + Name); return Name; } /*** * 获取Cookies值 * @param data * @return */ private String getCookiesValue(String data) { String Value = null; int sy = 0; data = data.trim(); if (data.contains("=")) { sy = data.indexOf("="); Value = data.substring(sy + 1, data.length()); } else { Value = ""; } Value = Value.trim(); // System.out.println("取Cookies值:" + Value); return Value; } /** * 整理Cookies * * @param odeck * @return */ private String finishingCookies(String odeck) { // 整理Cookies String newck = null, Name = null, Value = null; String[] data = null; data = odeck.split(";"); data = array_unique(data); for (int i = 0; i < data.length; i++) { Name = getCookiesName(data[i]); Value = getCookiesValue(data[i]); if (Name.isEmpty() == false && Value.isEmpty() == false) { newck = newck + Name + "=" + Value + "; "; } } return newck; } /*** * 返回状态码 * @return */ public String Get_Status() { return Status; } /*** * 网页访问 * @param urlNameString 请求url * @param data 请求数据 * @param way 请求方式 * @param cookies 请求cookies * @param xieyi 请求协议头 * @return */ public byte[] sendData(String urlNameString, String data, String way, String cookies, String xieyi) { String result = ""; String[] strarr = null; String xieyitou = ""; BufferedReader in = null; byte[] returnBytes=null; try { way = way.toUpperCase(); URL realUrl = new URL(urlNameString); URLConnection connection = realUrl.openConnection(); if (xieyi.isEmpty()) xieyitou = "Accept: */*\n"; if (xieyi.contains("Accept") == false && xieyi.isEmpty() == false) { xieyitou = xieyitou + "\nAccept: */*\n"; } if (xieyi.isEmpty() == false) xieyitou = xieyi + "\n"; if (xieyi.contains("Referer:") == false) xieyitou = xieyitou + "Referer:" + urlNameString + "\n"; if (xieyi.contains("Accept-Language:") == false) xieyitou = xieyitou + "Accept-Language: zh-cn\n"; if (xieyi.contains("User-Agent:") == false) xieyitou = xieyitou + "User-Agent: Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 6.1)\n"; if (xieyi.contains("Content-Type:") == false) xieyitou = xieyitou + "Content-Type: application/x-www-form-urlencoded\n"; if (cookies != null) connection.setRequestProperty("Cookie", cookies); // System.out.print("附加协议头:\n" + xieyitou + "\n"); strarr = xieyitou.split("\n"); for (int i = 0; i < strarr.length; i++) { connection.setRequestProperty(getXYName(strarr[i]), getXYValu(strarr[i])); } System.out.println("访问方式:" + way + "\n"); switch (way) { case "GET": connection.connect(); break; case "POST": connection.setDoOutput(true); connection.setDoInput(true); PrintWriter out = null; out = new PrintWriter(connection.getOutputStream()); // System.out.println("发送数据:\n"+data ); out.print(data); out.flush(); // System.out.println("发送数据:\n"+data ); break; default: break; } f_xieyitou = ""; Status = connection.getHeaderField("Status"); System.out.println("请求状态码:" + Status); Map<String, List<String>> map = connection.getHeaderFields(); String value; for (String key : map.keySet()) { value = map.get(key).toString().replace("[", ""); value = value.replace("]", ""); f_xieyitou += key + ": " + value + "\n"; } f_cookies = f_xieyitou.substring(f_xieyitou.indexOf("Set-Cookie:") + 11, f_xieyitou.indexOf("\n", f_xieyitou.indexOf("Set-Cookie:"))).trim(); f_cookies = f_cookies.replace("Path=/,", ""); f_cookies = f_cookies.replace("Path=/", ""); f_cookies = f_cookies.replace("path=/", ""); f_cookies = f_cookies.replace("Path=", ""); f_cookies = f_cookies.replace(",", ";"); f_cookies = finishingCookies(f_cookies); // System.out.println(way+"返回协议头:\n"+f_xieyitou); ByteArrayOutputStream outs = new ByteArrayOutputStream(); InputStream ins = connection.getInputStream(); byte[] tempdatas = new byte[1024]; int len=-1; while((len=ins.read(tempdatas,0,tempdatas.length))!=-1){ outs.write(tempdatas,0,len); } returnBytes=outs.toByteArray(); } catch (Exception e) { System.err.println("发送GET请求出现异常!" + e); e.printStackTrace(); return null; } // 使用finally块来关闭输入流 finally { try { if (in != null) { in.close(); } } catch (Exception e2) { e2.printStackTrace(); } } return returnBytes; }}
阅读全文
1 0
- java简单仿winhttp 发送get post请求
- winhttp 发送 get 请求
- Java发送Get/Post请求
- Java发送GET,POST请求
- JAVA发送POST+GET请求
- java发送get/post请求
- Java 发送 get, post 请求
- JAVA 发送 POST、GET请求
- Java发送get,post请求
- java发送GET、POST请求
- Java 发送get post请求
- JAVA 发送 POST、GET请求
- Java使用HttpURLConnection发送Get及Post请求简单过程
- JAVA 发送Http请求,GET、POST请求
- HttpClient发送Get、Post请求简单实践
- java发送HTTP GET POST请求
- 使用Java发送GET、POST请求
- 使用Java发送POST、GET请求【转】
- 2017多校联合第三场/hdu6059 Kanade's trio(tire tree)
- Javaweb--- EL表达式 JSTL标准标签库
- bzoj2882 工艺
- Dijkstra算法之地铁修建
- 统计一个字符串中字母出现的次数
- java简单仿winhttp 发送get post请求
- bzoj2176: Strange string
- codeforces 55D. Beautiful numbers
- Android APK反编译就这么简单 详解
- 带你一步步走入Paxos的世界 -- 序列1
- 虚拟机Linux系统的MySQL在本地的Navicat连不上
- 树莓派-可以适应显示器的boot/config.txt配置
- kNN_手写识别系统代码实现
- 个人总结与目标