本文主要是介绍JavaSE_URLConnection/HttpURLConnection发送HTTP请求的方法(一),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
原文地址: https://www.cnblogs.com/h--d/p/5495524.html
Java原生的API可用于发送HTTP请求
即java.net.URL、java.net.URLConnection,JDK自带的类;
发送请求的流程:
1.通过统一资源定位器(java.net.URL)获取连接器(java.net.URLConnection)
2.设置请求的参数
3.发送请求
4.以输入流的形式获取返回内容
5.关闭输入流
package com.util;import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Iterator;
import java.util.Map;public class HttpConnectionUtil {// post请求public static final String HTTP_POST = "POST";// get请求public static final String HTTP_GET = "GET";// utf-8字符编码public static final String CHARSET_UTF_8 = "utf-8";// HTTP内容类型。如果未指定ContentType,默认为TEXT/HTMLpublic static final String CONTENT_TYPE_TEXT_HTML = "text/xml";// HTTP内容类型。相当于form表单的形式,提交暑假public static final String CONTENT_TYPE_FORM_URL = "application/x-www-form-urlencoded";// 请求超时时间public static final int SEND_REQUEST_TIME_OUT = 50000;// 将读超时时间public static final int READ_TIME_OUT = 50000;/*** * @param requestType* 请求类型* @param urlStr* 请求地址* @param body* 请求发送内容* @return 返回内容*/public static String requestMethod(String requestType, String urlStr, String body) {// 是否有http正文提交boolean isDoInput = false;if (body != null && body.length() > 0)isDoInput = true;OutputStream outputStream = null;OutputStreamWriter outputStreamWriter = null;InputStream inputStream = null;InputStreamReader inputStreamReader = null;BufferedReader reader = null;StringBuffer resultBuffer = new StringBuffer();String tempLine = null;try {// 统一资源URL url = new URL(urlStr);// 连接类的父类,抽象类URLConnection urlConnection = url.openConnection();// http的连接类HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;// 设置是否向httpUrlConnection输出,因为这个是post请求,参数要放在// http正文内,因此需要设为true, 默认情况下是false;if (isDoInput) {httpURLConnection.setDoOutput(true);httpURLConnection.setRequestProperty("Content-Length", String.valueOf(body.length()));}// 设置是否从httpUrlConnection读入,默认情况下是true;httpURLConnection.setDoInput(true);// 设置一个指定的超时值(以毫秒为单位)httpURLConnection.setConnectTimeout(SEND_REQUEST_TIME_OUT);// 将读超时设置为指定的超时,以毫秒为单位。httpURLConnection.setReadTimeout(READ_TIME_OUT);// Post 请求不能使用缓存httpURLConnection.setUseCaches(false);// 设置字符编码httpURLConnection.setRequestProperty("Accept-Charset", CHARSET_UTF_8);// 设置内容类型httpURLConnection.setRequestProperty("Content-Type", CONTENT_TYPE_FORM_URL);// 设定请求的方法,默认是GEThttpURLConnection.setRequestMethod(requestType);// 打开到此 URL 引用的资源的通信链接(如果尚未建立这样的连接)。// 如果在已打开连接(此时 connected 字段的值为 true)的情况下调用 connect 方法,则忽略该调用。httpURLConnection.connect();if (isDoInput) {outputStream = httpURLConnection.getOutputStream();outputStreamWriter = new OutputStreamWriter(outputStream);outputStreamWriter.write(body);outputStreamWriter.flush();// 刷新}if (httpURLConnection.getResponseCode() >= 300) {throw new Exception("HTTP Request is not success, Response code is " + httpURLConnection.getResponseCode());}if (httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {inputStream = httpURLConnection.getInputStream();inputStreamReader = new InputStreamReader(inputStream);reader = new BufferedReader(inputStreamReader);while ((tempLine = reader.readLine()) != null) {resultBuffer.append(tempLine);resultBuffer.append("\n");}}} catch (MalformedURLException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();} finally {// 关闭流try {if (outputStreamWriter != null) {outputStreamWriter.close();}} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}try {if (outputStream != null) {outputStream.close();}} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}try {if (reader != null) {reader.close();}} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}try {if (inputStreamReader != null) {inputStreamReader.close();}} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}try {if (inputStream != null) {inputStream.close();}} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}return resultBuffer.toString();}/*** 将map集合的键值对转化成:key1=value1&key2=value2 的形式* * @param parameterMap* 需要转化的键值对集合* @return 字符串*/public static String convertStringParamter(Map parameterMap) {StringBuffer parameterBuffer = new StringBuffer();if (parameterMap != null) {Iterator iterator = parameterMap.keySet().iterator();String key = null;String value = null;while (iterator.hasNext()) {key = (String) iterator.next();if (parameterMap.get(key) != null) {value = (String) parameterMap.get(key);} else {value = "";}parameterBuffer.append(key).append("=").append(value);if (iterator.hasNext()) {parameterBuffer.append("&");}}}return parameterBuffer.toString();}public static void main(String[] args) throws MalformedURLException {System.out.println(requestMethod(HTTP_GET, "http://127.0.0.1:8080/test/TestHttpRequestServlet","username=123&password=我是谁"));}
}
这篇关于JavaSE_URLConnection/HttpURLConnection发送HTTP请求的方法(一)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!