java从本地向另外一个地址发送请求

这篇博客介绍了如何在Java开发中实现向其他服务发送GET请求。文章通过一个具体的代码示例,展示了如何在Service层封装一个抽象类来简化请求的发送过程,并提醒注意URL的拼接和JSON转换问题。此外,还提到了读取配置文件`config.properties`的注意事项。

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

1.说明

现实开发中我们可能要请求别人的借口,需要向别人的项目发起get请求,下面简单实现下怎么向别人的项目发起get请求。

2.代码示例

一般情况这种业务逻辑我们都写在service层,为了给service层提供方便我们把这种向别人发送的请求封装成了抽象类,方便调用代码如下:

package net.th2w.web.pay.service;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.HttpClientUtils;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Value;

public abstract class AbstractService {
	
	//↓↓↓↓↓↓↓↓↓这里是楼主项目中需要的参数 请忽略↓↓↓↓↓↓↓↓↓
	protected @Value(value = "${partnerId}") String PARTNERID;
	protected @Value(value = "${secret}") String SECRET;
	protected @Value(value = "${contentId}") String CONTENTID;
	protected @Value(value = "${channelId}") String CHANNELID;
	//↑↑↑↑↑↑↑↑这里是楼主项目中需要的参数 请忽略↑↑↑↑↑↑↑↑↑↑
	/**
	 * get 请求
	 * @param @param url
	 * @return String   
	 * @throws
	 */
	public static String doGetStr(String url){
		DefaultHttpClient httpClient = new DefaultHttpClient();
		HttpGet httpGet = new HttpGet(url);
		String result = "";
		
		try {
			HttpResponse response= httpClient.execute(httpGet);
			HttpEntity entity = response.getEntity();
			if(entity != null){
				result = EntityUtils.toString(entity,"UTF-8");
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			HttpClientUtils.closeQuietly(httpClient);
		}
		return result;
	}
	
	/**
	 * post 请求
	 * @param @param url
	 * @param @param outStr
	 * @throws
	 */
	public static String doPostStr(String url, String outStr){
		DefaultHttpClient httpClient = new DefaultHttpClient();
		HttpPost httpPost = new HttpPost();
		String result = "";
		
		try {
			httpPost.setEntity(new StringEntity(outStr,"UTF-8"));
			HttpResponse response = httpClient.execute(httpPost);
			result = EntityUtils.toString(response.getEntity(),"UTF-8");
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			HttpClientUtils.closeQuietly(httpClient);
		}
		return result;
	}
}
调用如下:

一定要继承AbstractService抽象类

@Service
public class LoginServiceImpl extends AbstractService implements LoginService{}
先继承再实现本身的service层的接口:

	public String getAccessToken(String code) {
		CodeInfo token = new CodeInfo();
		ResultJson resultJson = new ResultJson();
		String accessToken = "";
		//组织url
		String url = ACCESSTOKEN_URL+"?code="+code;
		//调用 
		String result = doGetStr(url);
		resultJson = JSON.parseObject(result, ResultJson.class);
		int codeflag = resultJson.getCode();
		String jsonData = resultJson.getData().toString();
		token = JSON.parseObject(jsonData, CodeInfo.class);
		System.out.println(token);
		try {
			//根据响应码判断是否拿到相应信息
			if(codeflag == 200){
				System.out.println("成功");
				accessToken = token.getAccess_token();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return accessToken;
	}

得到的result是一个JSON字符串。

3、总结

本地向别的项目发起get请求,注意拼接的url,还有JSON转换问题。

PS:补充一下我读取参数的配置文件config.properties

h5.login.url=http://passport.h5.1862.cn/open/mobile/login
h5.accessToken.url=http://passport.h5.1862.cn/open/access_token
h5.userInfo.url=http://passport.h5.1862.cn/open/user
h5.orderno.url=http://localhost:8080/passport/user/register
h5.pay.url=http://localhost:8080/passport/account/pay/apply

partnerId=0000000054231ba50154271fab8b00b0
secret=DG~ETHPfdhbr
contentId=0000000054231ba50154272247b400b1
channelId=0000000054231ba50154271e289c00ae

注意要读取config.properties这个配置文件需要在spring配置中配置一下

<context:property-placeholder location="classpath:config.properties" />


不忘初心,方得始终!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值