中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

當(dāng)前位置: 首頁 > news >正文

怎么用電腦做web服務(wù)器網(wǎng)站淘寶關(guān)鍵詞排名是怎么做的

怎么用電腦做web服務(wù)器網(wǎng)站,淘寶關(guān)鍵詞排名是怎么做的,手機(jī)網(wǎng)站服務(wù)器,wordpress 寵物模板最近接手一個(gè)項(xiàng)目,新項(xiàng)目需要調(diào)用老項(xiàng)目的接口,但是老項(xiàng)目和新項(xiàng)目不再同一個(gè)域名下,所以必須進(jìn)行跨域調(diào)用了,但是老項(xiàng)目又不能進(jìn)行任何修改,所以jsonp也無法解決了,于是想到了使用了Httpclient來進(jìn)行服務(wù)端…

最近接手一個(gè)項(xiàng)目,新項(xiàng)目需要調(diào)用老項(xiàng)目的接口,但是老項(xiàng)目和新項(xiàng)目不再同一個(gè)域名下,所以必須進(jìn)行跨域調(diào)用了,但是老項(xiàng)目又不能進(jìn)行任何修改,所以jsonp也無法解決了,于是想到了使用了Httpclient來進(jìn)行服務(wù)端的“跨域”來替代jsonp的客戶端跨域方案。

上一篇博文中,詳細(xì)剖析了jsonp的跨域原理,本文使用Httpclient來替代jsonp的客戶端跨域方案。

先去 http://hc.apache.org/downloads.cgi 下載最新版httpclient。解壓tutorial文件夾中有html和PDF的使用介紹。

下面實(shí)現(xiàn)從8888端口的html4項(xiàng)目中跨域訪問8080端口的html5項(xiàng)目中的JsonServlet:

1)在html4中建立一個(gè)中間代理servelt和一個(gè)工具類,工具類代碼如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

import?java.io.IOException;

import?java.io.OutputStream;

import?org.apache.http.HttpEntity;

import?org.apache.http.StatusLine;

import?org.apache.http.client.ClientProtocolException;

import?org.apache.http.client.HttpResponseException;

import?org.apache.http.client.methods.CloseableHttpResponse;

import?org.apache.http.client.methods.HttpPost;

import?org.apache.http.impl.client.CloseableHttpClient;

import?org.apache.http.impl.client.HttpClients;

public?class?HttpUtil

{

????public?static?boolean?returnResponseOfUrl(String url, OutputStream os)

????{

????????CloseableHttpClient httpclient = HttpClients.createDefault();

????????HttpPost httpPost =?new?HttpPost(url);

????????CloseableHttpResponse response =?null;

????????try{

????????????response = httpclient.execute(httpPost);

?????????????

????????????StatusLine statusLine = response.getStatusLine();

????????????HttpEntity entity = response.getEntity();

????????????if(statusLine !=?null?&& statusLine.getStatusCode() >=?300){

????????????????throw?new?HttpResponseException(statusLine.getStatusCode(),

????????????????????????????????????????????????statusLine.getReasonPhrase());

????????????}

????????????if(entity ==?null){

????????????????throw?new?ClientProtocolException("response contains no content");

????????????}

?????????????

????????????entity.writeTo(os);

????????????return?true;

????????}catch(IOException e){

????????????e.printStackTrace();

????????????return?false;

????????}finally{

????????????if(response !=?null){

????????????????try{

????????????????????response.close();

????????????????}catch(IOException e){

????????????????????e.printStackTrace();

????????????????}

????????????}

????????}

????}

}

?中間代理servlet代碼如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

@WebServlet("/HttpclientServlet")

public?class?HttpclientServlet?extends?HttpServlet

{

????private?static?final?long?serialVersionUID = 1L;

????????

????public?HttpclientServlet()

????{

????????super();

????}

????protected?void?doGet(HttpServletRequest request, HttpServletResponse response)?throws?ServletException, IOException

????{

????????this.doPost(request, response);

????}

????protected?void?doPost(HttpServletRequest request, HttpServletResponse response)?throws?ServletException, IOException

????{

????????String url = request.getParameter("url");

????????if(url !=?null){

????????????if(!HttpUtil.returnResponseOfUrl(url, response.getOutputStream())){

????????????????if(!HttpUtil.returnResponseOfUrl(url, response.getOutputStream())){?// 如果出錯(cuò),再試一次

????????????????????// log.error("url:" + url);

????????????????};?

????????????}

????????}

????}

}

?html4項(xiàng)目中的訪問頁面代碼如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

<!doctype?html>

<html>

<head>

????<meta?charset="utf-8">

????<meta?name="keywords" content="jsonp">

????<meta?name="description" content="jsonp">

????<title>jsonp</title>

????<style?type="text/css">

????????*{margin:0;padding:0;}

????????div{width:600px;height:100px;margin:20px auto;}

????</style>

</head>

<body>

????<div>

????????<a?href="javascript:;">jsonp測(cè)試</a>

????</div>

?????

<script?type="text/javascript" src="js/jquery-1.11.1.js"></script>

<script?type="text/javascript">

$(function(){

????$("a").on("click", function(){?????

????????$.ajax({

????????????type:"post",

????????????url:"http://localhost:8888/html4/HttpclientServlet?url="+ecodeURIComponent("http://localhost:8080/html5/JsonServlet"),

????????????success:function(data) {

????????????????console.log(data);

????????????????console.log(data.name);

????????????????console.log(data.age);

????????????????var user = JSON.parse(data);

????????????????console.log(user.name);

????????????????console.log(user.age);

????????????}

????????});

????})

});

</script>

</body>

</html>

上面通過:url=http://localhost:8080/html5/JsonServlet 將我們最終要跨域訪問的url地址傳給自己服務(wù)器下的 HttpclientServlet. 然后在 HttpclientServlet?中使用httpclient訪問 跨域 url? 中的servlet,成功之后,將返回的結(jié)果返回給客戶端。

html5項(xiàng)目中被 跨域 訪問的servlet代碼如下:

@WebServlet("/JsonServlet")
public class JsonServlet extends HttpServlet 
{private static final long serialVersionUID = 4335775212856826743L;protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {User user = new User();user.setName("yuanfang");user.setAge(100);Object obj = JSON.toJSON(user);System.out.println(user);            // com.tz.servlet.User@164ff87System.out.println(obj);            // {"age":100,"name":"yuanfang"}response.getWriter().println(obj);}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {this.doPost(request, response);}
}

啟動(dòng)8888和8080端口的tomcat,訪問 http://localhost:8888/html4/jsonp.html ,結(jié)果如下:

我們注意到第二和第三項(xiàng)都打印的是 undefined ,這是因?yàn)?中間代理的 HttpclientServlet,使用的是直接輸出流的方式,所以最終返回的結(jié)果不是Json對(duì)象,而是字符串,所以需要使用?var user = JSON.parse(data);?來進(jìn)行解析成 javascript對(duì)象就可以,所以第四和第五項(xiàng)都正常輸出了結(jié)果。

如果想返回的是json對(duì)象,加一句代碼?response.setContentType("text/json;charset=utf-8");?就可以:

1

2

3

4

5

6

7

8

if(url !=?null){

????response.setContentType("text/json;charset=utf-8");

????if(!HttpUtil.returnResponseOfUrl(url, response.getOutputStream())){

????if(!HttpUtil.returnResponseOfUrl(url, response.getOutputStream())){?// 如果出錯(cuò),再試一次

????????// log.error("url:" + url);

????????};?

????}

}???

這樣的話,瀏覽器在看到 contentType: "text/json;charset=utf-8" 時(shí),它的js執(zhí)行引擎會(huì)自動(dòng)幫助我們將字符串解析成json對(duì)象。也就是相當(dāng)于自動(dòng)調(diào)用了 JSON.parse(data) 的效果

http://www.risenshineclean.com/news/42359.html

相關(guān)文章:

  • 西安專業(yè)做網(wǎng)站建設(shè)費(fèi)用愛站網(wǎng)的關(guān)鍵詞是怎么來的
  • 溫州網(wǎng)站制作多少錢全網(wǎng)營(yíng)銷公司
  • 網(wǎng)站備案 godaddyseo公司上海牛巨微
  • 公司網(wǎng)站建設(shè)費(fèi)屬于什么費(fèi)用網(wǎng)站服務(wù)器是什么意思
  • 做返利網(wǎng)站怎麼網(wǎng)絡(luò)推廣費(fèi)用預(yù)算表
  • 大連在哪個(gè)網(wǎng)站做網(wǎng)上核名鄭州seo公司哪家好
  • 技術(shù)支持:淄博網(wǎng)站建設(shè)濰坊自動(dòng)seo
  • 線上推廣方法有哪些長(zhǎng)沙網(wǎng)站seo報(bào)價(jià)
  • 期貨做程序化回測(cè)的網(wǎng)站網(wǎng)站網(wǎng)絡(luò)排名優(yōu)化方法
  • 建設(shè)綜合信息網(wǎng)站需要多少錢如何廣告推廣
  • 國(guó)產(chǎn)一級(jí)a做爰片免費(fèi)網(wǎng)站哪個(gè)網(wǎng)站是免費(fèi)的
  • 網(wǎng)絡(luò)營(yíng)銷模式包括哪些seo網(wǎng)站關(guān)鍵詞快速排名
  • 做網(wǎng)站放太多視頻seo項(xiàng)目分析
  • 十堰網(wǎng)站seo方法百度seo關(guān)鍵詞優(yōu)化公司
  • 做公司網(wǎng)站一般多少錢免費(fèi)軟件下載網(wǎng)站有哪些
  • 集團(tuán)網(wǎng)站建設(shè)方案書游戲推廣員是違法的嗎
  • 軟件開發(fā)步驟流程鄭州見效果付費(fèi)優(yōu)化公司
  • 廈門 微網(wǎng)站制作企業(yè)推廣策劃書
  • 做寵物食品的網(wǎng)站優(yōu)化落實(shí)疫情防控新十條
  • 上傳了網(wǎng)站源碼怎么做新聞最新熱點(diǎn)
  • 桓臺(tái)網(wǎng)站開發(fā)廣州:推動(dòng)優(yōu)化防控措施落地
  • 中國(guó)互聯(lián)網(wǎng)網(wǎng)站性能丈哥seo博客工具
  • 錦州網(wǎng)站建設(shè)多少錢網(wǎng)站排名掉了怎么恢復(fù)
  • wordpress 地址 .html臺(tái)州seo
  • 別人抄襲網(wǎng)站設(shè)計(jì)怎么辦設(shè)計(jì)師必備的6個(gè)網(wǎng)站
  • 尋花問柳一家專注做男人喜愛的網(wǎng)站什么網(wǎng)站推廣比較好
  • 諸城盟族網(wǎng)站建設(shè)北京做網(wǎng)站公司哪家好
  • 網(wǎng)上營(yíng)銷活動(dòng)長(zhǎng)沙網(wǎng)站seo分析
  • 學(xué)校校園網(wǎng)站建設(shè)方案上海網(wǎng)站營(yíng)銷seo方案
  • 做圖片的網(wǎng)站外貿(mào)建站