本文目录
- Java客户端调用Webservice接口流程
- java调用WebService接口为什么这么慢
- java如何调用webservice接口
- java程序怎么调用webservice接口,实现发送短信功能
- java怎样发布webservice接口详细步骤
Java客户端调用Webservice接口流程
给你看看以前写的获取电话号码归属地的代码的三种方法,然后你就懂了。
import java.io.ByteArrayOutputStream;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.URL;import org.apache.commons.httpclient.HttpClient;import org.apache.commons.httpclient.HttpException;import org.apache.commons.httpclient.methods.PostMethod;public class MobileCodeService { public void httpGet(String mobile,String userID) throws Exception { //http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo?mobileCode=string&userID=string URL url = new URL(“http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo?mobileCode=“+mobile+“&userID=“+userID); HttpURLConnection conn =(HttpURLConnection)url.openConnection(); conn.setConnectTimeout(5000); conn.setRequestMethod(“GET“); if(conn.getResponseCode()==HttpURLConnection.HTTP_OK) //200 { InputStream is= conn.getInputStream(); ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream(); // byte buf = new byte; int len = -1; while((len = is.read(buf))!=-1) { //获取结果 arrayOutputStream.write(buf, 0, len); } System.out.println(“Get方式获取的数据是:“+arrayOutputStream.toString()); arrayOutputStream.close(); is.close(); } } public void httpPost(String mobile,String userID) throws HttpException, IOException { //访问路径 http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo //HttpClient访问 HttpClient httpClient = new HttpClient(); PostMethod pm = new PostMethod(“http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo“); pm.setParameter(“mobileCode“, mobile); pm.setParameter(“userID“, userID); int code= httpClient.executeMethod(pm); System.out.println(“状态码:“+code); //获取结果 String result = pm.getResponseBodyAsString(); System.out.println(“获取到的数据是:“+result); } public void SOAP() throws Exception { HttpClient client = new HttpClient(); PostMethod method = new PostMethod(“http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx“); //设置访问方法的参数 method.setRequestBody(new FileInputStream(“C:\\soap.xml“)); method.setRequestHeader(“Content-Type“,“text/xml; charset=utf-8“); int code= client.executeMethod(method); System.out.println(“状态码:“+code); //获取结果 String result = method.getResponseBodyAsString(); System.out.println(“获取到的数据是:“+result); } public static void main(String args) throws Exception { MobileCodeService mcs=new MobileCodeService(); mcs.httpGet(“18524012513“, ““); //mcs.httpPost(“18524012513“, ““); //mcs.SOAP(); }}java调用WebService接口为什么这么慢
与网络有关吧!现在最简单的就是用eclipse把wsdl生成java客户端代码,然后直接调用1,直接AXIS调用远程的web service我觉得这种方法比较适合那些高手,他们能直接看懂XML格式的WSDL文件,我自己是看不懂的,尤其我不是专门搞这行的,即使一段时间看懂,后来也就忘记了。直接调用模式如下:import java.util.Date;import java.text.DateFormat;import org.apache.axis.client.Call;import org.apache.axis.client.Service;import javax.xml.namespace.QName;import java.lang.Integer;import javax.xml.rpc.ParameterMode; public class caClient {public static void main(String args) { try {String endpoint = “
java如何调用webservice接口
Java通过WSDL文件来调用webservice直接调用模式如下:
import java.util.Date;
import java.text.DateFormat;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import javax.xml.namespace.QName;
import java.lang.Integer;
import javax.xml.rpc.ParameterMode;
public class caClient {
public static void main(String args) {
try {
String endpoint = “http://localhost:8080/ca3/services/caSynrochnized?wsdl“;
//直接引用远程的wsdl文件
//以下都是套路
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(endpoint);
call.setOperationName(“addUser“);//WSDL里面描述的接口名称
call.addParameter(“userName“, org.apache.axis.encoding.XMLType.XSD_DATE,
javax.xml.rpc.ParameterMode.IN);//接口的参数
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);//设置返回类型
String temp = “测试人员“;
String result = (String)call.invoke(new Object{temp});
//给方法传递参数,并且调用方法
System.out.println(“result is “+result);
}
catch (Exception e) {
System.err.println(e.toString());
}
}
}
java程序怎么调用webservice接口,实现发送短信功能
java程序怎么调用webservice接口,实现发送短信功能首先我访问不到你提供的webservice你可以直接找那个给你webservice的人对他提供的方法进行询问不知你是什么项目框架所以不能给出具体的方案,如果是spring的话直接在配置文件中进行配置就可。首先必须获得客户端以及地址,客户端提供的是接口的定义及规范,地址是要我们进行连接的。例如:《bean id=“XXService“ class=“com.xx.客户端接口“ factory-bean=“XXServiceFactory“ factory-method=“create“ /》 《bean id=“XXServiceFactory“ class=“org.apache.cxf.jaxws.JaxWsProxyFactoryBean“》 《property name=“serviceClass“ value=“com.xx.客户端接口“ /》 《property name=“address“ value=“地址/》 《/bean》然后你就可以在你的业务层进行注入,这个是采用cxf的方式,当然也可以有其他方式
java怎样发布webservice接口详细步骤
用netbeans建项目的时候,选择webservice就可以了。//你用main函数能启动一个新端口?既然用main函数能行,就把main函数功能,写到一个类的构造函数中,把这个类配置到spring配置文件中,spring启动的时候自然会装载类,装载类的时候自然就能执行到这部分代码了。