HttpURLConnection

get
public static String getDataByUrl1(String url)
			throws UnsupportedEncodingException, IOException {
		URL getUrl = new URL(url);
		HttpURLConnection connection = (HttpURLConnection) getUrl
				.openConnection();
		System.out.println("connection.getResponseCode()"+connection.getResponseCode());
        if(connection.getResponseCode() == 200) {  //如果成功返回  
        	System.out.println("111");
        }  
        System.out.println("4");
		// 取得输入流,并使用Reader读取
		BufferedReader reader = new BufferedReader(new InputStreamReader(
				connection.getInputStream(), "utf-8")); // 设置编码,否则中文乱码
		String lines = "";
		StringBuilder temp1 = new StringBuilder();
		while ((lines = reader.readLine()) != null) {
			temp1.append(lines);
		}
		reader.close();
        
		// 断开连接
		connection.disconnect();
		return new String(temp1);
	}

post

public static String sendPostRequest(String inter,
			Map<String, String> params) throws Exception {
		InputStream is = null;
		String json = "";
		String path = URL + inter;
		System.out.println("path--->" + path);
		StringBuilder sb = new StringBuilder();
		if (params != null && !params.isEmpty()) {
			for (Map.Entry<String, String> entry : params.entrySet()) {
				sb.append(entry.getKey()).append('=')
						.append(URLEncoder.encode(entry.getValue(), "utf-8"))
						.append('&');
			}
			sb.deleteCharAt(sb.length() - 1);
		}
		// 得到实体的二进制数据,以便计算长度
		byte[] entitydata = sb.toString().getBytes();
		URL url = new URL(path);
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		conn.setRequestMethod("POST");
//		conn.setConnectTimeout(5 * 1000);
		conn.setDoOutput(true);// 如果通过post提交数据,必须设置允许对外输出数据
		// Content-Type: application/x-www-form-urlencoded
		// Content-Length: 38
		// 下面的两个属性是必须的
		conn.setRequestProperty("Content-Type",
				"application/x-www-form-urlencoded");
		conn.setRequestProperty("Content-Length",
				String.valueOf(entitydata.length)); // 传递数据的长据
		OutputStream outStream = conn.getOutputStream();
		outStream.write(entitydata);
		// 把内存中的数据刷新输送给对方
		outStream.flush();
		outStream.close();
		// 获取服务端的响应,200代表成功
		System.out.println("state--->" + conn.getResponseCode());
		if (conn.getResponseCode() == 200) {
			is = conn.getInputStream();

			// json = NetUtils.readString(is);
			System.out.println("成功");
			// 下面的json就已经是{"Person":{"username":"zhangsan","age":"12"}}
			// //这个形式了,只不过是String类型
		} else {
			is = conn.getInputStream();
			// json = NetUtils.readString(is);
			System.out.println("失败");
			System.out.println("json--->" + json);
		}
		String r = NetUtils.readString(is);
		System.out.println("r-->"+r);
		conn.disconnect();
		return r;
	}
例:http://shmed.eicp.net:8065/handeservice.asmx/HealthPADBus?XmlString=<Body><Request OrgCode='5' OperType='28'><Province ID='11'></Province></Request></Body>

get方式提交时需要将参数值进行encode---------URLEncoder.encode(value,"utf-8");http://shmed.eicp.net:8065/handeservice.asmx/HealthPADBus?XmlString=%3CBody%3E%3CRequest+OrgCode%3D%275%27+OperType%3D%2728%27%3E%3CProvince+ID%3D%2711%27%3E%3C%2FProvince%3E%3C%2FRequest%3E%3C%2FBody%3E   不然会报400!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值