HttpURLConnection接口实例

package com.test.test.web.test;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Map;

import org.apache.activemq.transport.stomp.ProtocolException;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.JsonSyntaxException;
import com.test.test.web.utils.MD5;

public class HttpURLConnection_test {
	
	/**
	 * HttpURLConnection
	 * @author 小屁文
	 */
	public static void main(String[] args) {
		MD5 md5 = new MD5();
		ObjectMapper objectMapper = new ObjectMapper();
		//设置参数
		String username = md5.md5String("username");
		String password = md5.md5String("password");
		String company = "中文编码";
		
		try {
			//给汉字设置编码格式
			company = URLEncoder.encode(company, "utf-8");
			String VERIFY = "username="+username+"&password="+password+"&company="+company;
			//URL地址
			String url = "http://127.0.0.1:8080/test/HttpURLConnection_test";  
			StringBuffer sb = new StringBuffer();
			//将String类型的地址转换为URL格式
			URL urls = new URL(url);
			//打开URL链接
			HttpURLConnection uc = (HttpURLConnection) urls.openConnection();
			//链接设置  传输方式:post 编码格式:utf-8
			uc.setRequestMethod("POST");
			//设定传送的内容类型是可序列化的java对象 s
			//如果不设此项,在传送序列化对象时,当WEB服务默认的不是这种类型时可能抛java.io.EOFException
			uc.setRequestProperty("content-type", "application/x-www-form-urlencoded");
			uc.setRequestProperty("Accept-Charset", "utf-8");
			uc.setRequestProperty("Content-Length", String.valueOf(VERIFY.length())); 
			// http正文内,因此需要设为true, 默认情况下是false;
			uc.setDoOutput(true);
			// 设置是否从httpUrlConnection读入,默认情况下是true
			uc.setDoInput(true);
			// Post 请求不能使用缓存
			uc.setUseCaches(false);
			//超时设置,防止 网络异常的情况下,可能会导致程序僵死而不继续往下执行
			//建立完成资源连接到开始读取数据的超时(单位:毫秒)
			uc.setReadTimeout(10000);
			//连接主机的超时时间(单位:毫秒)
			uc.setConnectTimeout(10000);
			// 连接,从上述url.openConnection()至此的配置必须要在connect之前完成,    
			//uc.connect();
			// 此处getOutputStream会隐含的进行connect(即:如同调用上面的connect()方法,    
			// 所以在开发中不调用上述的connect()也可以)。
			OutputStream os = uc.getOutputStream();
			DataOutputStream dos = new DataOutputStream(os);
			dos.writeBytes(VERIFY);
			dos.flush();
			os.close();
			BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream(), "utf-8"));
			String readLine = "";
			while ((readLine = in.readLine()) != null) {
				sb.append(readLine);
			}
			in.close();
			String respJson = sb.toString();
			//将返回的json对象转换为list
			Map<String, Object> map = objectMapper.readValue(respJson, Map.class);
			System.out.println("map:"+map);
			//处理数据
//			if(map != null){
//				String error = map.get("error").toString();           //返回结果状态
//				String error_info = map.get("message").toString();   //返回结果状态说明
//			}
		} catch (JsonSyntaxException e) {
			e.printStackTrace();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (ProtocolException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值