http 请求,get,post,put请求方式,《利用HttpClient》

简介:利用nginx 跳过证书,然后java代码调用nginx

工具类请求代码:


	/** 
	 * httpClient的get请求方式
	 * 
	 * @return
	 * @throws Exception
	 */
	public String doGet(String url) throws Exception {
		/* 1 生成 HttpClinet 对象并设置参数 */
		HttpClient httpClient = new HttpClient();
		GetMethod getMethod = new GetMethod(url.trim());
		getMethod.setRequestHeader("Content-Type", "application/json");
		getMethod.setRequestHeader("Host",“放”);
		getMethod.setRequestHeader("Authorization", "Token "+bizConf.get("INDelhivery_Token").trim());
		String response = "";
		/* 2 执行 HTTP GET 请求 */
		try {
			int statusCode = httpClient.executeMethod(getMethod);
			/* 3 判断访问的状态码 */
			if (statusCode != HttpStatus.SC_OK) {
				System.err.println("请求出错: " + getMethod.getStatusLine());
			}
			// 读取 HTTP 响应内容,这里简单打印网页内容
			byte[] responseBody = getMethod.getResponseBody();// 读取为字节数组
			response = new String(responseBody, "UTF-8");
		} finally {
			/* 4 .释放连接 */
			getMethod.releaseConnection();
		}
		return response;
	}
	
	/**
	 * httpClient的post请求方式
	 * 
	 * @return
	 * @throws Exception
	 */
	public String doPost(String jsonData, String url) throws Exception {
		String postResult = "";
		//第一步定义好httpClient
		HttpClient httpClient = new HttpClient();
		//第二步定义好postMethod
		PostMethod postMethod = new PostMethod(url.trim());
		//第三步定义头
		postMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		postMethod.setRequestHeader("Host", bizConf.get("INDelhivery_Host"));
		postMethod.setRequestHeader("Authorization", "Token "+bizConf.get("INDelhivery_Token").trim());
		//第四步封装请求参数
		postMethod.setParameter("format", "json");
		postMethod.setParameter("data", jsonData);
		try {
			int statusCode = httpClient.executeMethod(postMethod);
			if (statusCode != HttpStatus.SC_OK) {
				logger.info("请求出错: " + postMethod.getStatusLine());
			}
			//第五步处理返回参数
			postResult = IOUtils.toString(postMethod.getResponseBodyAsStream(), "UTF-8");
		} finally {
			//第六步 释放连接 
			postMethod.releaseConnection();
		}
		return postResult;
	}

public String doPut(String jsonData, String url) throws Exception {
		String postResult = "";
		HttpClient httpClient = new HttpClient();
		PutMethod putMethod = new PutMethod(url.trim());
		putMethod.setRequestHeader("Content-Type", "application/json");
		putMethod.setRequestHeader("Accept", "*/*");
		putMethod.setRequestHeader("User-Agent", "Mozilla 5.0");
		putMethod.setRequestHeader("Host", "digitalapi.auspost.com.au");
		String accountNumber=bizConf.get("AUPost_accountNumber");
		putMethod.setRequestHeader("account-number", accountNumber);
		putMethod.setRequestHeader("Cache-Control", "no-cache");

		// 获取加密结果并转BASE64
		putMethod.setRequestHeader("Authorization", getHeader());
		// 封装请求参数
		StringRequestEntity requestEntity = new StringRequestEntity(jsonData, "application/json", "UTF-8");
		putMethod.setRequestEntity(requestEntity);
		try {
			StringBuffer sb = new StringBuffer();
			int statusCode = httpClient.executeMethod(putMethod);
			InputStream is = putMethod.getResponseBodyAsStream();
			byte[] b = new byte[1024];
			int len = 0;
			while((len = is.read(b)) > 0) {
				sb.append(new String(b, 0, len));
			}
			postResult = sb.toString();
		} finally {
			putMethod.releaseConnection();
		}
		return postResult;
	}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值