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
- java从本地向另外一个地址发送请求
- MFC从一个对话框向另外一个对话框发送消息
- java 向HTTPS地址发送POST请求
- 向指定地址发送get、post请求
- HttpClient向HTTPS地址发送POST请求
- AJAX - 向服务器发送一个请求
- java向别的地址发送参数和请求,并解析结果
- Java向Web站点发送POST请求
- Java向Web站点发送POST请求
- Java向Web站点发送POST请求
- java 向nodejs 发送请求简单案例
- java向https链接发送post请求
- 关于从另外一个页面请求传值问题
- 使用HttpClient向HTTPS地址发送POST请求4.1.2
- 向nginx发送请求
- 向服务器发送请求
- Java快速从一个文件夹复制到另外一个文件夹
- 一个Ajax请求如何向服务器发送XML
- 注解初体验与自定义注解实战
- struct、union
- 《疯狂Java讲义(第3版)》.(李刚)——前言
- 对于酷派手机无法打印log问题解决方法
- Timus 1820. Ural Steaks
- java从本地向另外一个地址发送请求
- IntentService
- 杭电5645
- linux驱动中实现上层select接口
- HDU - 5665 Lucky (技巧)
- 写博客的初衷
- validator验证指定字符
- 精确毫秒
- B-树与B+树