Jsonp和HttpClient跨域请求

跨域请求

一、JsonP

1.概念:

Jsonp其实就是一个跨域解决方案。Js跨域请求数据是不可以的,但是js跨域请求js脚本是可以的。可以把数据封装成一个js语句,做一个方法的调用。跨域请求js脚本可以得到此脚本。得到js脚本之后会立即执行。可以把数据做为参数传递到方法中。就可以获得数据。从而解决跨域问题

2.原理

浏览器在js请求中,允许通过script标签的src跨域请求,可以在请求的结果中添加回调方法名,在请求页面中定义方法,可获取到跨域请求的数据。

 aaa

 

3.实现

  A. 请求的url必须有一个回调函数的名字的请求参数

 aaa

  B:使用jsonp来实现跨域请求

aaa

注:必须有一个category.getDataService的回调函数

 aaa

 

 

 C:服务端返回的json数据必须是个js片段,(一个名字为请求参数的名字的函数的js片段)类似于:

category.getDataService(Json对象);

 

二、使用HttpClient跨域请求(客户端使用进行跨域请求)

1.导包:

 

二、实现代码部分

 

//Get请求

@Test

public void doGet()throws Exception{

//创建HttpClient

CloseableHttpClient httpClient = HttpClients.createDefault();

//创建get 请求

HttpGet get = new HttpGet("http://www.sogou.com");

//执行请求

CloseableHttpResponse response = httpClient.execute(get);

//获取请求结果

int code = response.getStatusLine().getStatusCode();

System.out.println(code);

HttpEntity entity = response.getEntity();

String result = EntityUtils.toString(entity, "utf-8");

System.out.println(result);

//关闭

response.close();

httpClient.close();

}

//Get请求带参数

@Test

public void doGetWithParam()throws Exception{

//创建HttpClient

CloseableHttpClient httpClient = HttpClients.createDefault();

//创建Get 请求

//创建URL对象

URIBuilder uriBuilder = new URIBuilder("http://www.sogou.com/web");

//添加请求参数

uriBuilder.addParameter("query", "花千骨");

HttpGet get = new HttpGet(uriBuilder.build());

//执行请求

CloseableHttpResponse response = httpClient.execute(get);

//获取请求结果

int code = response.getStatusLine().getStatusCode();

System.out.println(code);

HttpEntity entity = response.getEntity();

String result = EntityUtils.toString(entity, "utf-8");

System.out.println(result);

//关闭

response.close();

httpClient.close();

}

//Post请求

@Test

public void doPost()throws Exception{

CloseableHttpClient httpClient = HttpClients.createDefault();

//创建POST请求

HttpPost post = new HttpPost("http://localhost:8089/rest/httpclient");

//执行请求

CloseableHttpResponse response = httpClient.execute(post);

int code = response.getStatusLine().getStatusCode();

System.out.println(code);

HttpEntity entity = response.getEntity();

String result = EntityUtils.toString(entity,"utf-8");

System.out.println(result);

}

//Post请求带参数

@Test

public void doPostWithParam()throws Exception{

CloseableHttpClient httpClient = HttpClients.createDefault();

//创建POST请求

HttpPost post = new HttpPost("http://localhost:8089/rest/httpclient/withparam");

post.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");

//设置请求头,中文乱码问题

//创建请求体

//new String("张三".getBytes("utf-8"),"utf-8"))

List<NameValuePair> list = new ArrayList<NameValuePair>();

list.add(new BasicNameValuePair("userName","张三"));

list.add(new BasicNameValuePair("passWord","112"));

//包装Entity

UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list,"utf-8");

            

//post.setHeader("Content-type", "application/json");

//设置请求体

post.setEntity(entity);

//执行请求

CloseableHttpResponse response = httpClient.execute(post);

int code = response.getStatusLine().getStatusCode();

System.out.println(code);

HttpEntity httpEntity = response.getEntity();

String result = EntityUtils.toString(httpEntity,"utf-8");

System.out.println(result);

}

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值