SpringBoot跨域请求调用工具类

SpringBoot跨域请求调用工具类

一、SpringBoot解决跨域请求调用。

详解。SpringBoot项目请求调用其他系统接口(简称:跨域)。

在这里插入图片描述

A服务系统请求调用B服务系统使用Ajax请求调用无效,原因是A服务系统和B服务系统属于独立系统,Ajax不具备跨域调用,需使用后端代码实现跨域请求调用从而实现A服务系统和B服务系统之间的调用。

二、详解范例,代码如下。

package com;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import org.apache.http.util.EntityUtils;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class text {

    
    
  public static void main(String args[]) throws Exception{
        final String CONTENT_TYPE_TEXT_JSON = "text/json";
        DefaultHttpClient client = new DefaultHttpClient(
                new PoolingClientConnectionManager());
        
        String url="";//跨域请求链接
      String js = "{\"ids\":\"c2bb87bd4f1d4619bec5a0b518485ceb\"}";//跨域请求参数JSON格式传输
      HttpPost httpPost = new HttpPost(url);       
      httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
      StringEntity se = new StringEntity(js);
      se.setContentType(CONTENT_TYPE_TEXT_JSON);
      httpPost.setEntity(se);
      CloseableHttpResponse response2 = null; 
      response2 = client.execute(httpPost);
      HttpEntity entity2 = null;
      entity2 = response2.getEntity();
      String s2 = EntityUtils.toString(entity2, "UTF-8");
      
      com.alibaba.fastjson.JSONObject deptDate = com.alibaba.fastjson.JSONObject.parseObject(s2);
      JSONObject jsonObject =new JSONObject();
      JSONObject jsonback =jsonObject.fromObject(deptDate);
      JSONArray jsonArrayBack =jsonback.getJSONArray("data");
      String sid = "";
      for (int i = 0; i < jsonArrayBack.size(); i++) {
        JSONObject json =jsonArrayBack.getJSONObject(i);
        sid =json.getString("sid").toString();
        
      }
      System.out.println(sid); 
    }
}

            1.3、示例,代码如下(隶属工具类,在方法中调用)。

package com.sgcc.uap.util;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import org.apache.http.util.EntityUtils;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class IscGetSid {

  public static String getIscSid(String url,String parameter) throws Exception{
        final String CONTENT_TYPE_TEXT_JSON = "text/json";
        DefaultHttpClient client = new DefaultHttpClient(
                new PoolingClientConnectionManager());
      HttpPost httpPost = new HttpPost(url);       
      httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
      StringEntity se = new StringEntity(parameter);
      se.setContentType(CONTENT_TYPE_TEXT_JSON);
      httpPost.setEntity(se);
      CloseableHttpResponse response2 = null; 
      response2 = client.execute(httpPost);
      HttpEntity entity2 = null;
      entity2 = response2.getEntity();
      String s2 = EntityUtils.toString(entity2, "UTF-8");
      
      com.alibaba.fastjson.JSONObject deptDate = com.alibaba.fastjson.JSONObject.parseObject(s2);
      JSONObject jsonObject =new JSONObject();
      JSONObject jsonback =jsonObject.fromObject(deptDate);
      JSONArray jsonArrayBack =jsonback.getJSONArray("data");
      String sid = "";
      for (int i = 0; i < jsonArrayBack.size(); i++) {
        JSONObject json =jsonArrayBack.getJSONObject(i);
        sid =json.getString("sid").toString();
        
      }
      return sid;
      
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

真心喜欢你吖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值