用cxf编写基于spring的webservice之下篇

简介: 版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010741376/article/details/48518843

上一篇,我们通过客户端的Java代码访问webservice,这一篇,我们通过HttpURLConnection请求webservice,如果使用ajax直接请求webservice,会存在跨域的问题,

比如:如果我发布的地址是localhost的,那么在页面的js里使用IP访问的话,就会有问题,访问不了。

页面请求的servlet代码:

package com.cxf.servlet;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class HttpURLConnectionServlet
 */
@WebServlet("/HttpURLConnectionServlet")
public class HttpURLConnectionServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public HttpURLConnectionServlet() {
        super();
    }
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String code=request.getParameter("code");
        System.out.println("code:"+code);
        //请求体
        String data="<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'><soap:Body><ns2:getOrderById xmlns:ns2='http://ws.cxf.com/'><arg0>"+code+"</arg0></ns2:getOrderById></soap:Body></soap:Envelope>";
		URL url=new URL("http://localhost:8088/cxf_spring_ws/ws/orderws");
		HttpURLConnection openConnection = (HttpURLConnection) url.openConnection();
		openConnection.setDoOutput(true);//是否从httpUrlConnection输出
		openConnection.setDoInput(true);// 设置是否从httpUrlConnection读入
		openConnection.setRequestMethod("POST");//请求类型
		openConnection.setRequestProperty("Content-Type","text/xml;charset=utf-8");
		OutputStream outputStream = openConnection.getOutputStream();
		outputStream.write(data.getBytes("utf-8"));//向服务端写入数据
		
		/**
		 * 当相应码为200的时候,说明请求成功
		 */
		int responseCode = openConnection.getResponseCode();
		if(responseCode==200){
			InputStream inputStream = openConnection.getInputStream();//读取服务端的数据
			System.out.println("return:"+inputStream.available());
			/**
			 * 将请求得到的数据写到页面
			 */
			byte[] buffer=new byte[1024];
			response.setContentType("text/xml;charset=utf-8");
			ServletOutputStream os = response.getOutputStream();
			int len;
			while((len=inputStream.read(buffer))>0){
				os.write(buffer, 0, len);
			}
			os.flush();
			
		}
		
		
		
		
		
	}

}

jsp页面:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">

	<script type="text/javascript" src="jquery-1.7.2.js"></script>
	<script type="text/javascript">
	     $(function(){
	    	 $("#bt").click(function(){
	    		var code=$("#code").val();
	    		 $.post(
	    		'HttpURLConnectionServlet',
	    		{"code":code},
	    		function(msg){
	    			var $msg=$(msg);
	    		     var info=$msg.find("return").text();
	    			alert(info);
	    		},
	    		'xml'
	    		 );
	    		 
	    	 });
	    	 
	     });
	</script>
  </head>
  <body>
         <input type="text" id="code">
          <input type="button" id="bt" value="调用webservice">
  </body>
  
  
</html>
这样就可以了。

相关文章
【Azure 应用服务】Web App Service 中的 应用程序配置(Application Setting) 怎么获取key vault中的值
【Azure 应用服务】Web App Service 中的 应用程序配置(Application Setting) 怎么获取key vault中的值
184 0
【Azure App Service】PowerShell脚本批量添加IP地址到Web App允许访问IP列表中
Web App取消公网访问后,只允许特定IP能访问Web App。需要写一下段PowerShell脚本,批量添加IP到Web App的允许访问IP列表里!
218 2
|
关系型数据库 MySQL Linux
【Azure 应用服务】在创建Web App Service的时候,选Linux系统后无法使用Mysql in App
【Azure 应用服务】在创建Web App Service的时候,选Linux系统后无法使用Mysql in App
125 0
【Azure 应用服务】在创建Web App Service的时候,选Linux系统后无法使用Mysql in App
|
缓存 JavaScript 前端开发
Web Workers与Service Workers:后台处理与离线缓存
Web Workers 和 Service Workers 是两种在Web开发中处理后台任务和离线缓存的重要技术。它们在工作原理和用途上有显著区别。
273 1
|
Shell PHP Windows
【Azure App Service】Web Job 报错 UNC paths are not supported. Defaulting to Windows directory.
【Azure App Service】Web Job 报错 UNC paths are not supported. Defaulting to Windows directory.
179 0
|
Linux 应用服务中间件 网络安全
【Azure 应用服务】查看App Service for Linux上部署PHP 7.4 和 8.0时,所使用的WEB服务器是什么?
【Azure 应用服务】查看App Service for Linux上部署PHP 7.4 和 8.0时,所使用的WEB服务器是什么?
143 0
【Azure 应用服务】通过 Web.config 开启 dotnet 应用的 stdoutLog 日志,查看App Service 产生500错误的原因
【Azure 应用服务】通过 Web.config 开启 dotnet 应用的 stdoutLog 日志,查看App Service 产生500错误的原因
277 0
|
Linux Python
【Azure 应用服务】Azure App Service For Linux 上实现 Python Flask Web Socket 项目 Http/Https
【Azure 应用服务】Azure App Service For Linux 上实现 Python Flask Web Socket 项目 Http/Https
266 0
|
存储 安全 网络安全
【Azure 环境】使用Azure中的App Service部署Web应用,以Windows为主机系统是否可以启动防病毒,防恶意软件服务呢(Microsoft Antimalware)?
【Azure 环境】使用Azure中的App Service部署Web应用,以Windows为主机系统是否可以启动防病毒,防恶意软件服务呢(Microsoft Antimalware)?
191 0
|
存储 Linux 网络安全
【Azure 应用服务】App Service For Linux 如何在 Web 应用实例上住抓取网络日志
【Azure 应用服务】App Service For Linux 如何在 Web 应用实例上住抓取网络日志
138 0