java简单仿winhttp 发送get post请求

xiaoxiao2021-02-27  119

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; } }
转载请注明原文地址: https://www.6miu.com/read-16478.html

最新回复(0)