当前位置 : 主页 > 编程语言 > java >

java发送get、post请求

来源:互联网 收集:自由互联 发布时间:2021-06-28
HttpRequestUtil.java package com.myUtil;import java.util.List;import java.io.BufferedReader;import java.io.File;import java.io.InputStreamReader;import java.io.PrintWriter;import java.net.InetSocketAddress;import java.net.Proxy;import java.
HttpRequestUtil.java
package com.myUtil;

import java.util.List;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;
import java.util.Map;

import org.apache.xmlbeans.impl.util.Base64;

public class HttpRequestUtil {
	
	private static Proxy createProxy(String proxyHost, int proxyPort) {
		// 创建代理服务器  
		InetSocketAddress addr = new InetSocketAddress(proxyHost, proxyPort);
		// http代理 
		Proxy proxy = new Proxy(Proxy.Type.HTTP, addr);
		return proxy;
	}
	
	public static String sendGet(String url)
	{
		return sendGet(url, null);
	}
	
	public static String sendGet(String url, String params)
	{
		String result = "";
		String urlName = "";
		BufferedReader br = null;
		try {
			if (params != "" && params != null) {
				urlName = url + "?" + params;
			}
			else {
				urlName = url;
			}
			URL realUrl = new URL(urlName);
			URLConnection conn = realUrl.openConnection();
			// 设置代理
			//URLConnection conn = realUrl.openConnection(createProxy(proxyHost, proxyPort));
			// 如果需要设置代理账号密码则添加下面一行
			//conn.setRequestProperty("Proxy-Authorization", "Basic "+Base64.encode("account:password".getBytes()));
			conn.setRequestProperty("accept", "*/*");
			conn.setRequestProperty("Connection", "Keep-Alive");
			conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.86 Safari/537.36");
			conn.connect();
			Map
 
  > map = conn.getHeaderFields();
			for(String key : map.keySet())
			{
				System.out.println(key + ": " + map.get(key));
			}
			br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
			String line;
			while((line = br.readLine()) != null)
			{
				result += line + "\n";
			}
		} catch (Exception e) {
			System.out.println("发送GET请求出现异常!" + e);
			e.printStackTrace();
		} finally {
			if(br != null)
			{
				try {
					br.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		return result;
	}
	
	public static String sendPost(String url, String formData) {
		return sendPost(url, null, formData);
	}
	
	public static String sendPost(String url, String params, String formData) {
		PrintWriter out = null;
		BufferedReader br = null;
		String urlName = "";
		String result = "";
		try {
			if (params != "" && params != null) {
				urlName = url + "?" + params;
			}
			else {
				urlName = url;
			}
			URL realURL = new URL(urlName);
			URLConnection conn = realURL.openConnection();
			// 设置代理
			//URLConnection conn = realUrl.openConnection(createProxy(proxyHost, proxyPort));
			// 如果需要设置代理账号密码则添加下面一行
			//conn.setRequestProperty("Proxy-Authorization", "Basic "+Base64.encode("account:password".getBytes()));
			conn.setRequestProperty("accept", "*/*");
			conn.setRequestProperty("Connection", "Keep-Alive");
			conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.86 Safari/537.36");
			// 如果需要设置代理账号密码则添加下面一行
			//conn.setRequestProperty("Proxy-Authorization", "Basic "+Base64.encode("account:password".getBytes()));
			// 发送POST请求必须设置如下两行
			conn.setDoOutput(true);
			conn.setDoInput(true);
			// 获取URLConnection对象对应的输入流
			out = new PrintWriter(conn.getOutputStream());
			// 发送请求参数
			out.print(formData);
			out.flush();
			br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
			String line;
			while((line = br.readLine()) != null) {
				result += line + "\n";
			}
		} catch (Exception e) {
			System.out.println("发送POST请求出现异常!" + e);
			e.printStackTrace();
		} finally {
			try {
				if(br != null)
				{
					br.close();
				}
				if(out != null)
				{
					out.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return result;
	}

}
 
网友评论