import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

import com.sys.common.log.Log;

/**
 * 
 */

/**
 * 使用HttpURLConnection对象模拟测试后台接口
 * @author zhangwei
 *
 */
public class MyHttpURLConnection {
	
	private static String HTTP_URL="http://www.baidu.com";
	private static String	HTTP_METHOD_POST="POST";
	
	
	
	public static void main(String[] args) {
		
		 
		 try {
		System.out.println(	MyHttpURLConnection.sendHttp(""));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	
	
	
	/**
	 * 模拟HTTP 请求 提交普通数据
	 * 
	 * @param postData
	 *            发送数据
	 * @return
	 */
	private static String sendHttp(String postData) throws Exception {
		if (HTTP_URL == null || "".equals(HTTP_URL)) {
			throw new Exception("请传入远程请求地址");
		}
		String result = "";
		BufferedWriter out = null;
		BufferedInputStream reader = null;
		HttpURLConnection connection = null;
		try {
			URL u = new URL(HTTP_URL);
			connection = (HttpURLConnection) u.openConnection();
			int code;
			connection.setDoOutput(true);//是否向connection输出  
			connection.setUseCaches(false);//因为是post请求所以不能使用缓存
			connection.setConnectTimeout(120000); // 连接超时为120秒
			connection.setRequestMethod(HTTP_METHOD_POST);
			//告诉服务端 要发送数据的长度
			connection.setRequestProperty("Content-Length",
					postData.getBytes("UTF-8").length + "");
			//告诉服务器客户端浏览器内核
			connection.setRequestProperty("User-Agent", "Mozilla/4.0");
			//告诉服务器 要发送数据的格式和字符编码
			connection.setRequestProperty("Content-Type",
					"text/xml;charset=UTF-8");
			//通过connection连接对象获取输出流对象
			out = new BufferedWriter(new OutputStreamWriter(
					connection.getOutputStream(), "UTF-8"));
			//将需要想客户端发送的信息写入到输出流  刷新输出流并且关闭流
			out.write(postData);
			out.flush();
			out.close();
			out = null;
			code = connection.getResponseCode();
			if (code == HttpURLConnection.HTTP_OK) {
				ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
				reader = new BufferedInputStream(connection.getInputStream());
				int b;
				byte[] bit = new byte[1024];
				while (-1 != (b = reader.read(bit))) {
					byteArrayOutputStream.write(bit, 0, b);
				}
				result = new String(byteArrayOutputStream.toByteArray());
			} else {
				Log.error("post ResponseCode=" + connection.getResponseCode());
				Log.error("post ResponseMessage="
						+ connection.getResponseMessage());
				reader = new BufferedInputStream(connection.getErrorStream());
				ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
				int b;
				byte[] bit = new byte[1024];
				while (-1 != (b = reader.read(bit))) {
					byteArrayOutputStream.write(bit, 0, b);
				}
				result = new String(byteArrayOutputStream.toByteArray());
				throw new Exception("服务端发生异常");
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			if (out != null) {
				out.flush();
				out.close();
			}
			if (reader != null) {
				reader.close();
			}
			if (connection != null) {
				connection.disconnect();
			}
		}
		return result;
	}

}
最近下载更多
郭亚钢  LV8 2021年3月8日
pxqtsht  LV16 2021年1月28日
Jackzhou  LV1 2020年5月12日
diandian3757  LV2 2020年5月10日
zhangsuo  LV1 2020年1月16日
hanxinjie  LV25 2019年12月13日
低调人  LV38 2019年8月3日
liujiaweijv  LV7 2019年5月16日
1662530129  LV12 2019年5月3日
泡泡跑跑泡泡  LV8 2019年1月13日
最近浏览更多
wuziayng1232  LV10 2023年2月20日
17782780328  LV1 2022年7月21日
liys1234  LV9 2022年4月27日
329512801  LV28 2021年4月8日
喵小兵  LV10 2021年3月13日
郭亚钢  LV8 2021年3月8日
pxqtsht  LV16 2021年1月28日
oo3534  LV2 2020年10月10日
落轩昂  LV12 2020年7月22日
Jackzhou  LV1 2020年5月12日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友