java调用http请求

原创 2016年08月31日 15:14:57
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.Map;

import org.springframework.util.StringUtils;

public class AccessUrlUtil {

	/**
	 * 访问url
	 * 
	 * @param url
	 * @param method
	 * @param map
	 * @return
	 */
	public static Object accessUrl(String url, String method, Map<String, String> map) throws Exception {
		method = (null == method || "".equals(method)) ? "GET" : method;
		StringBuffer params = new StringBuffer("");
		if (null != map && !map.isEmpty()) {
			Iterator<String> keys = map.keySet().iterator();
			while (keys.hasNext()) {
				String key = keys.next();
				params.append(encode(key, map.get(key))).append('&');
			}
		}
		URL localURL = new URL(url);
		URLConnection connection = localURL.openConnection();
		HttpURLConnection httpURLConnection = (HttpURLConnection) connection;
		httpURLConnection.setDoOutput(true);
		httpURLConnection.setRequestMethod(method);
		httpURLConnection.setRequestProperty("Accept-Charset", "utf-8");
		httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
		httpURLConnection.setRequestProperty("Content-Length", String.valueOf(params.length()));

		OutputStream outputStream = null;
		OutputStreamWriter outputStreamWriter = null;
		InputStream inputStream = null;
		InputStreamReader inputStreamReader = null;
		BufferedReader reader = null;
		StringBuffer resultBuffer = new StringBuffer("");
		String tempLine = null;

		try {
			outputStream = httpURLConnection.getOutputStream();
			outputStreamWriter = new OutputStreamWriter(outputStream);

			outputStreamWriter.write(params.toString());
			outputStreamWriter.flush();

			if (httpURLConnection.getResponseCode() >= 300) {
				throw new Exception("HTTP Request is not success, Response code is " + httpURLConnection.getResponseCode());
			}

			inputStream = httpURLConnection.getInputStream();
			inputStreamReader = new InputStreamReader(inputStream);
			reader = new BufferedReader(inputStreamReader);

			while ((tempLine = reader.readLine()) != null) {
				resultBuffer.append(tempLine);
			}

		} finally {

			if (outputStreamWriter != null) {
				outputStreamWriter.close();
			}

			if (outputStream != null) {
				outputStream.close();
			}

			if (reader != null) {
				reader.close();
			}

			if (inputStreamReader != null) {
				inputStreamReader.close();
			}

			if (inputStream != null) {
				inputStream.close();
			}

		}
		return resultBuffer.toString();

	}

	private static String encode(String key, String value) throws UnsupportedEncodingException {
		return URLEncoder.encode(key, "UTF-8") + '=' + URLEncoder.encode(value, "UTF-8");
	}
}

版权声明:本文为博主原创文章,未经博主允许不得转载。 举报

相关文章推荐

Java调用Http请求

package test;import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamRe...

Java http请求和调用

关于http get和post请求调用代码以及示例。 参考:http://www.cnblogs.com/zhuawang/archive/2012/12/08/2809380.htmlhttp请求...

精选:深入理解 Docker 内部原理及网络配置

网络绝对是任何系统的核心,对于容器而言也是如此。Docker 作为目前最火的轻量级容器技术,有很多令人称道的功能,如 Docker 的镜像管理。然而,Docker的网络一直以来都比较薄弱,所以我们有必要深入了解Docker的网络知识,以满足更高的网络需求。

java调用http请求

java中调用http请求方法封装,参数为url,method,params。直接调用即可,返回调用返回的数据

JAVA发送HTTP请求,并接受返回内容

/** * Copyright 2017 * 注意:请勿修改 * All right reserved. */ package test; import java.io.BufferedR...

http几种请求方式调用

首先本文涉及的http发送请求有:‘get’,‘post’,‘put’,’delete‘。 一、表单方式 一般的form表单支持两种请求方式:‘get’,和‘post’,写法如下: ...

API & HTTP请求调试插件(Postman)

简述Postman是一个Chrome扩展,提供功能强大的Web API & HTTP请求调试。它能够发送任何类型的HTTP 请求(GET、HEAD、POST、PUT..),附带任何数量的参数+head...

JAVA HTTP 请求

JAVA HTTP 请求

java 请求http

package com.xx.emidas.activityvip.action.externalRequest; import java.io.BufferedReader; import ja...

java调用Http请求 -HttpURLConnection学习

最常用的Http请求无非是get和post,get请求可以获取静态页面,也可以把参数放在URL字串后面,传递给servlet,post与get的不同之处在于post的参数不是放在URL字串里面,而是放...
  • sd0902
  • sd0902
  • 2013-08-20 10:10
  • 30339

java http请求

package test; import java.io.BufferedReader; import java.io.InputStreamReader; import org.apache....
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)