java HttpClient方式
import com.alibaba.fastjson.JSONObject
import org.apache.http.HttpResponse
import org.apache.http.HttpStatus
import org.apache.http.client.methods.HttpPost
import org.apache.http.entity.StringEntity
import org.apache.http.impl.client.CloseableHttpClient
import org.apache.http.impl.client.HttpClientBuilder
import org.apache.http.util.EntityUtils
import java.io.IOException
import java.util.HashMap
import java.util.Map
public class ExportTest {
public static void main(String[] args) throws IOException {
Map<String, Object> params = new HashMap<>()
params.put("townId", 1)
params.put("hallOpertionType", "ALL")
JSONObject json = new JSONObject(params)
System.out.println(doPost("https://operationgwtest.bgycc.com/phantom-service-subscribe/subscribe/hall/exportexcel", json))
}
public static JSONObject doPost(String url, JSONObject json){
CloseableHttpClient httpclient = HttpClientBuilder.create().build()
HttpPost post = new HttpPost(url)
JSONObject response = null
try {
StringEntity s = new StringEntity(json.toString())
s.setContentEncoding("UTF-8")
s.setContentType("application/json")
post.setEntity(s)
HttpResponse res = httpclient.execute(post)
if(res.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
String result = EntityUtils.toString(res.getEntity())
response = JSONObject.parseObject(result)
}
} catch (Exception e) {
throw new RuntimeException(e)
}
return response
}
}
js XMLHttpRequest方式 普通请求
var xhr = null;
try {
xhr = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
try {
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e1) {
xhr = new XMLHttpRequest();
}
}
xhr.open("post", "http://10.187.58.26:7012/phantom-service-subscribe/subscribe/hall/exportexcel", true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onreadystatechange = function() {
console.log(xhr.responseText);
if (xhr.readyState == 4 && xhr.status == 200) {
console.log(xhr.responseText);
}
};
var content = '{"townId":1,"hallOpertionType":"ALL"}';
xhr.send(content);
js XMLHttpRequest方式 文件下载
var xhh = new XMLHttpRequest();
xhh.open("post", "http://10.187.58.26:7012/phantom-service-subscribe/subscribe/hall/exportexcel");
xhh.setRequestHeader("Content-Type", "application/json");
xhh.responseType = 'blob';
xhh.onreadystatechange = function () {
if (xhh.readyState === 4 && xhh.status === 200) {
var filename = "文件名.xls";
var blob = new Blob([xhh.response], {type: 'text/xls'});
var csvUrl = URL.createObjectURL(blob);
var link = document.createElement('a');
link.href = csvUrl;
link.download = filename;
link.click();
}
};
var content = '{"townId":1,"hallOpertionType":"ALL"}';
xhh.send(content);