http学习
- httpserver 简单示例(参考:https://www.cnblogs.com/exmyth/p/7524440.html)
import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.InetSocketAddress; import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpHandler; import com.sun.net.httpserver.HttpServer; public class MyHTTPServer { public static void main(String[] args) { try { //实现HTTP SERVER HttpServer hs = HttpServer.create(new InetSocketAddress(8888),0);// 设置HttpServer的端口为80 hs.createContext("/hujun", new MyHandler());// 用MyHandler类内处理到/的请求 hs.setExecutor(null); // creates a default executor hs.start(); //实现HTTPS SERVER HttpsServer hss = HttpsServer.create(new InetSocketAddress(443),0); //设置HTTPS端口这443 KeyStore ks = KeyStore.getInstance("JKS"); //建立证书库 ks.load(new FileInputStream("证书名" ), "密码"); //载入证书 KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509"); //建立一个密钥管理工厂 kmf.init(ks, "密码"); //初始工厂 SSLContext sslContext = SSLContext.getInstance("SSLv3"); //建立证书实体 sslContext.init(kmf.getKeyManagers(), null, null); //初始化证书 HttpsConfigurator conf = new HttpsConfigurator(sslContext); //在https配置 hss.setHttpsConfigurator(conf); //在https server载入配置 hss.setExecutor(null); // creates a default executor hss.createContext("/", new MyHandler());// 用MyHandler类内处理到/的请求 hss.start(); } catch (Exception e){ e.printStackTrace(); } } } class MyHandler implements HttpHandler { public void handle(HttpExchange t) throws IOException { InputStream is = t.getRequestBody(); String response = "<font color='#ff0000'>come on baby</font>"; t.sendResponseHeaders(200, response.length()); OutputStream os = t.getResponseBody(); os.write(response.getBytes()); os.close(); } }
- httpclient 代码示例(参考:https://blog.csdn.net/justry_deng/article/details/81042379)
/** * POST---有参测试(普通参数) * * @date 2018年7月13日 下午4:18:50 */ @Test public void doPostTestFour() { // 获得Http客户端(可以理解为:你得先有一个浏览器;注意:实际上HttpClient与浏览器是不一样的) CloseableHttpClient httpClient = HttpClientBuilder.create().build(); // 参数 StringBuffer params = new StringBuffer(); try { // 字符数据最好encoding以下;这样一来,某些特殊字符才能传过去(如:某人的名字就是“&”,不encoding的话,传不过去) params.append("name=" + URLEncoder.encode("&", "utf-8")); params.append("&"); params.append("age=24"); } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); } // 创建Post请求 HttpPost httpPost = new HttpPost("http://localhost:12345/doPostControllerFour" + "?" + params); // 设置ContentType(注:如果只是传普通参数的话,ContentType不一定非要用application/json) httpPost.setHeader("Content-Type", "application/json;charset=utf8"); // 响应模型 CloseableHttpResponse response = null; try { // 由客户端执行(发送)Post请求 response = httpClient.execute(httpPost); // 从响应模型中获取响应实体 HttpEntity responseEntity = response.getEntity(); System.out.println("响应状态为:" + response.getStatusLine()); if (responseEntity != null) { System.out.println("响应内容长度为:" + responseEntity.getContentLength()); System.out.println("响应内容为:" + EntityUtils.toString(responseEntity)); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { // 释放资源 if (httpClient != null) { httpClient.close(); } if (response != null) { response.close(); } } catch (IOException e) { e.printStackTrace(); } } }
3. http理论讲解,参考:https://blog.csdn.net/w372426096/article/details/82713315