httpClient中post请求并传送form-data数据

本文详细介绍了如何使用Java实现POST和GET请求,包括设置代理、超时时间及处理响应数据。通过具体代码示例,展示了如何构建请求参数、发送请求并解析返回结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

     开发中遇到对接需求时候,被要求用post请求传form-data数据的时候一脸懵逼,最后经过多重摸索百度后终于找到方法,废话不多说,直接上代码。

​
​

public StringBuffer connection(Map<String,String> map, String strURL) {
		// start
		HttpClient httpClient = new HttpClient();
		 
		httpClient.getHostConfiguration().setProxy("10.192.10.101", 8080); //设置代理
		httpClient.getParams().setAuthenticationPreemptive(true);  

		HttpConnectionManagerParams managerParams = httpClient
				.getHttpConnectionManager().getParams();
		// 设置连接超时时间(单位毫秒)
		managerParams.setConnectionTimeout(30000);
		// 设置读数据超时时间(单位毫秒)
		managerParams.setSoTimeout(120000);

		PostMethod postMethod = new PostMethod(strURL);
		// 将请求参数XML的值放入postMethod中
		String strResponse = null;
		StringBuffer buffer = new StringBuffer();
		// end
		try {
			//设置参数到请求对象中,重点是map中有几个参数NameValuePair数组也必须设置成几,不然
            //会空指针异常
			NameValuePair[] nvps = new NameValuePair[4];
			int index = 0;
			for(String key : map.keySet()){
				nvps[index++]=new NameValuePair(key, map.get(key));
			}
			postMethod.setRequestBody(nvps);
			int statusCode = httpClient.executeMethod(postMethod);
			if (statusCode != HttpStatus.SC_OK) {
				throw new IllegalStateException("Method failed: "
						+ postMethod.getStatusLine());
			}
			BufferedReader reader = null;
			reader = new BufferedReader(new InputStreamReader(
					postMethod.getResponseBodyAsStream(), "UTF-8"));
			while ((strResponse = reader.readLine()) != null) {
				buffer.append(strResponse);
			}
		} catch (Exception ex) {
			throw new IllegalStateException(ex.toString());
		} finally {
			// 释放连接
			postMethod.releaseConnection();
		}
		return buffer;
	}

​

​

另附上get请求代码:

​
public String httpGetInfo(String strURL) throws Exception{
		CloseableHttpClient httpCilent2 = HttpClients.createDefault();
		HttpHost proxy = new HttpHost("10.192.10.101", 8080);
		RequestConfig requestConfig = RequestConfig.custom()
				.setProxy(proxy)//设置代理
				.setConnectTimeout(30000)   //设置连接超时时间
				.setConnectionRequestTimeout(120000) // 设置请求超时时间
				.setSocketTimeout(120000)
				.setRedirectsEnabled(true)//默认允许自动重定向
				.build();
		HttpGet httpGet2 = new HttpGet(strURL);
		httpGet2.setConfig(requestConfig);
		String srtResult = "";
		try {
			HttpResponse httpResponse = httpCilent2.execute(httpGet2);
			if(httpResponse.getStatusLine().getStatusCode() == 200){
				srtResult = EntityUtils.toString(httpResponse.getEntity());//获得返回的果
				System.out.println(srtResult);
			}else if(httpResponse.getStatusLine().getStatusCode() == 400){
				//..........
			}else if(httpResponse.getStatusLine().getStatusCode() == 500){
				//.............
			}
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			try {
				httpCilent2.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return srtResult;
	}

​

 

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值