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;
}
}