Jsonp和HttpClient跨域请求
跨域请求
一、JsonP
1.概念:
Jsonp其实就是一个跨域解决方案。Js跨域请求数据是不可以的,但是js跨域请求js脚本是可以的。可以把数据封装成一个js语句,做一个方法的调用。跨域请求js脚本可以得到此脚本。得到js脚本之后会立即执行。可以把数据做为参数传递到方法中。就可以获得数据。从而解决跨域问题
2.原理
浏览器在js请求中,是允许通过script标签的src跨域请求,可以在请求的结果中添加回调方法名,在请求页面中定义方法,既可获取到跨域请求的数据。
3.实现
A. 请求的url必须有一个回调函数的名字的请求参数
B:使用jsonp来实现跨域请求
注:必须有一个category.getDataService的回调函数
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); } |