标签:iterator random code bst put method style url puts
1 package com.xiwi; 2 import java.io.BufferedReader; 3 import java.io.IOException; 4 import java.io.InputStreamReader; 5 import java.io.OutputStreamWriter; 6 import java.net.HttpURLConnection; 7 import java.net.URL; 8 import java.net.URLEncoder; 9 import java.security.KeyManagementException; 10 import java.security.NoSuchAlgorithmException; 11 import java.security.SecureRandom; 12 import java.util.HashMap; 13 import java.util.Iterator; 14 import java.util.Map; 15 import javax.net.ssl.HostnameVerifier; 16 import javax.net.ssl.HttpsURLConnection; 17 import javax.net.ssl.SSLContext; 18 import javax.net.ssl.SSLSession; 19 import javax.net.ssl.TrustManager; 20 21 /** 22 * Xiwi 23 * HttpURLConnection类 封装 24 * 25 */ 26 public class HttpUtils { 27 private URL httpUrl = null; 28 private HttpURLConnection httpConn = null; 29 30 private Map<String, String> httpHeaders = null; 31 private String httpCookie = null; 32 private String httpResult = null; 33 34 // 类的初始化 设置对HTTPS请求的支持 35 public HttpUtils ( ) throws NoSuchAlgorithmException, KeyManagementException { 36 SSLContext sslcontext = SSLContext.getInstance("SSL"); 37 TrustManager[] tm = {new MyX509TrustManager()}; 38 sslcontext.init(null, tm, new SecureRandom()); 39 HostnameVerifier ignoreHostnameVerifier = new HostnameVerifier() { 40 public boolean verify ( String s, SSLSession sslsession ) { 41 System.out.println("WARNING: Hostname is not matched for cert."); 42 return true; 43 } 44 }; 45 HttpsURLConnection.setDefaultHostnameVerifier(ignoreHostnameVerifier); 46 HttpsURLConnection.setDefaultSSLSocketFactory(sslcontext.getSocketFactory()); 47 } 48 49 50 public Map<String, String> getHeaders ( ) { return httpHeaders; } 51 public void setHeader ( Map<String, String> header ) { 52 if ( httpHeaders == null ) httpHeaders = new HashMap<String, String>(); 53 Iterator<String> iterHeaders = header.keySet().iterator(); 54 while ( iterHeaders.hasNext() ) { 55 String key = iterHeaders.next(); 56 String value = header.get(key); 57 httpHeaders.put(key, value); 58 } 59 } 60 61 public String getCookie ( ) { return httpCookie; } 62 public void setCookie ( String cookie ) { 63 Map<String, String> map = new HashMap<String, String>(); 64 map.put("Cookie", cookie); 65 setHeader(map); 66 } 67 68 69 // GET 请求 70 public String sendGet ( String url ) throws IOException { 71 72 httpUrl = new URL(url); 73 httpConn = (HttpURLConnection)httpUrl.openConnection(); 74 75 send("GET", null); 76 77 System.out.println("get ok"); 78 return httpResult; 79 } 80 81 // POST 请求 82 public String sendPost ( String url, Map<String, String> params ) throws IOException { 83 84 httpUrl = new URL(url); 85 httpConn = (HttpURLConnection)httpUrl.openConnection(); 86 87 send("POST", params); 88 89 System.out.println("post ok"); 90 return httpResult; 91 } 92 93 94 private void send ( String method, Map<String, String> params ) throws IOException { 95 96 httpConn.setRequestMethod(method); 97 98 httpConn.setDoInput(true); 99 httpConn.setDoOutput(true); 100 httpConn.setConnectTimeout(5000); 101 102 // 判断 是否有设置 请求头 103 if ( httpHeaders != null ) { 104 Iterator<String> iter = httpHeaders.keySet().iterator(); 105 while ( iter.hasNext() ) { 106 String key = iter.next(); 107 System.out.print("key: " + key); 108 System.out.print("\t"); 109 System.out.println("value: " + httpHeaders.get(key)); 110 httpConn.setRequestProperty(key, httpHeaders.get(key)); 111 } 112 } 113 114 // 判断 是否有设置请求数据 115 if ( params != null ) { 116 StringBuffer data = new StringBuffer(); 117 Iterator<String> iter = params.keySet().iterator(); 118 while ( iter.hasNext() ) { 119 String key = iter.next(); 120 String value = params.get(key); 121 data.append(URLEncoder.encode(key, "UTF-8")); 122 data.append("=").append( 123 URLEncoder.encode(value, "UTF-8")); 124 data.append("&"); 125 } 126 127 OutputStreamWriter writer = new OutputStreamWriter(httpConn.getOutputStream()); 128 writer.write(data.substring(0, data.length() - 1)); 129 writer.flush(); 130 writer.close(); 131 132 /* 提交数据方式 第二种 133 PrintWriter printWriter = new PrintWriter(httpConn.getOutputStream()); 134 printWriter.write(data.substring(0, data.length() - 1)); 135 printWriter.flush(); 136 printWriter.close(); 137 */ 138 /* 提交数据方式 第三种 139 DataOutputStream dos=new DataOutputStream(httpConn.getOutputStream()); 140 dos.writeBytes(data.substring(0, data.length() - 1)); 141 dos.flush(); 142 dos.close(); 143 */ 144 /* 提交数据方式 第四种 145 OutputStream outwritestream = httpConn.getOutputStream(); 146 outwritestream.write(data.substring(0, data.length() - 1).getBytes()); 147 outwritestream.flush(); 148 outwritestrean.close(); 149 */ 150 System.out.println("data: " + data.substring(0, data.length() - 1)); 151 } 152 153 154 //System.out.println("返回代码: " + httpConn.getResponseCode()); 155 if ( httpConn.getResponseCode() == 200 ) { 156 157 // 保存 Cookie 158 httpCookie = httpConn.getHeaderField("Set-Cookie"); 159 if ( httpCookie == null || httpCookie.equals("") ) { 160 httpCookie = httpConn.getHeaderField("Cookie"); 161 } 162 163 // 保存 网页请求头,跳过第一个 HTTP/1.1 164 Map<String, String> mapHeaders = new HashMap<String, String>(); 165 Iterator<String> iterHeader = httpConn.getHeaderFields().keySet().iterator(); 166 int i = 1; 167 while ( iterHeader.hasNext() ) { 168 String key = iterHeader.next(); 169 if ( i == 1 ) {continue;} 170 mapHeaders.put(key, httpConn.getHeaderField(key)); 171 i++; 172 } 173 setHeader(mapHeaders); 174 175 // 获取网页返回值 176 BufferedReader bf = new BufferedReader(new InputStreamReader(httpConn.getInputStream())); 177 String line = ""; 178 httpResult = ""; 179 while ( (line = bf.readLine()) != null ) { 180 httpResult += line + "\n"; 181 } 182 bf.close(); 183 } 184 else { 185 // 请求失败 则 返回网页请求代码 186 httpResult = httpConn.getResponseCode() + ""; 187 } 188 189 httpConn.disconnect(); 190 } 191 192 193 194 public String test ( String msg ) { 195 return msg; 196 } 197 198 199 200 201 }
标签:iterator random code bst put method style url puts
原文地址:https://www.cnblogs.com/Xiwi/p/Xiwi.html