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

来源:互联网 发布:mac怎么下载qq游戏 编辑:程序博客网 时间:2024/06/12 00:32

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抽象类

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

public String getAccessToken(String code) {CodeInfo token = new CodeInfo();ResultJson resultJson = new ResultJson();String accessToken = "";//组织urlString 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/loginh5.accessToken.url=http://passport.h5.1862.cn/open/access_tokenh5.userInfo.url=http://passport.h5.1862.cn/open/userh5.orderno.url=http://localhost:8080/passport/user/registerh5.pay.url=http://localhost:8080/passport/account/pay/applypartnerId=0000000054231ba50154271fab8b00b0secret=DG~ETHPfdhbrcontentId=0000000054231ba50154272247b400b1channelId=0000000054231ba50154271e289c00ae

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

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


不忘初心,方得始终!

0 0
原创粉丝点击