❶ java語言使用post方式調用webService方式
WebService可以有Get、Post、Soap、Document四種方式調用,以下Java通過方式調用WebService代碼:
importjava.io.BufferedReader;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.InputStreamReader;
importjava.io.OutputStream;
importjava.io.OutputStreamWriter;
importjava.net.URL;
importjava.net.URLConnection;
importjava.net.URLEncoder;
importorg.apache.cxf.endpoint.Client;
importorg.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
/**
*功能描述:WebService調用
*
*/
publicclassClientTest{
/**
*功能描述:HTTP-POST
*
*/
publicStringpost(){
OutputStreamWriterout=null;
StringBuildersTotalString=newStringBuilder();
try{
URLurlTemp=newURL(
"http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getSupportCity");
URLConnectionconnection=urlTemp.openConnection();
connection.setDoOutput(true);
out=newOutputStreamWriter(connection.getOutputStream(),"UTF-8");
StringBuffersb=newStringBuffer();
sb.append("byProvinceName=福建");
out.write(sb.toString());
out.flush();
StringsCurrentLine;
sCurrentLine="";
InputStreaml_urlStream;
l_urlStream=connection.getInputStream();//請求
BufferedReaderl_reader=newBufferedReader(newInputStreamReader(
l_urlStream));
while((sCurrentLine=l_reader.readLine())!=null){
sTotalString.append(sCurrentLine);
}
}catch(Exceptione){
e.printStackTrace();
}finally{
if(null!=out){
try{
out.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
}
returnsTotalString.toString();
}
}
❷ java調用webservice介面的時候報錯,(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String什麼意思
一般來說,這是處理異常的方法失當造成的,本來原始的 exception 是帶著類名的,類似於 InvalidMethodException:版 javax.xml.ws.soap.SOAPFaultException: com.aisino.wx.listener.PostWX.post_wx(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String 等,但被包裝成 SOAPFaultException 之後就權失去了 InvalidMethodException 這個關鍵的信息。所以剩下的事情就要靠猜了。或者服務端是否能看到日誌?一般來說這是個不可恢復的錯誤,也就是說你重試多少次都是這個錯誤。
上次 InvalidMethodException 只是用來舉例的,真實的異常類型我們需要查看調用時的日誌,如果你的代碼是從客戶端直接發起 SOAP 調用,它的日誌就在客戶端,如果請求是從伺服器端發起的日誌就在伺服器端。
❸ 現在java調用webservice是用什麼技術
JAVA調用WS介面現在用的比較多就是AXIS和CXF了
最早的時候是使用AXIS的比較多,因為這個是最早支持專JAVA的WS介面的,像ECLIPSE里都自帶了屬AXIS,然後因為AXIS很久沒有更新了,這時候CXF慢慢進入大家眼中
CXF的介面實現起來更簡單,和其它語言實現的介面互相調用的時候兼容性也很好,再加上還有REST可以更簡單的訪問資源,現在很多新項目都會考慮用CXF,但是有很多老項目還是用的AXIS,如果維護的話也得能看懂,所以還是可以兩個都應該學學,必竟就是實現的方式有些不同而已,原理都是差不多的
關於CXF這個我之前找到一個哥們寫的一些很不錯的筆記,如果有興趣的話你可以去參考參考 http://my.oschina.net/huangyong/blog/294324
❹ java調用webservice,命名空間和方法名指的是什麼
1.xml
2. wsdl: webservice description language web服務描述語言
通過xml格式說明調用的地址方法如何調用,可以看錯webservice的說明書
3.soap simple object access protoacl (簡單對象訪問協議)
限定了xml的格式
soap 在http(因為有請求體,所以必須是post請求)的基礎上傳輸xml數據
請求和響應的xml 的格式如: <Envelop>
<body>
//....
</body>
</Envelop>
operation name:服務提供的方法
靜態方法不能發布為外部服務
運用jkd自帶的代碼生成訪問伺服器的客戶端代碼 E:/wsimort -s . http://test.cm/?wsdl
我們可以把webservice看做是web伺服器上的一個應用,web伺服器是webservice的一個容器
函數的參數在 http://test.cm/?xsd=1
JAX-WS是指 java api for xml -WebService
//測試 WebService服務的 explorer
Web Service Explorer 可以顯示返回的xml格式
targetNamespace 默認為倒置的包名
客戶端調用WebService的方式:
1.通過wximport生成代碼
2.通過客戶端編程方式
3.通過ajax調用方式
4.通過 URL Connection 方式調用
請求過程分析:
1.使用get方式獲取wsdl文件,稱為握手
2.使用post發出請求
3.伺服器響應成功過
❺ Java客戶端調用Webservice介面流程
給你看看以前寫的獲取電話號碼歸屬地的代碼的三種方法,然後你就懂了。
importjava.io.ByteArrayOutputStream;
importjava.io.FileInputStream;
importjava.io.IOException;
importjava.io.InputStream;
importjava.net.HttpURLConnection;
importjava.net.URL;
importorg.apache.commons.httpclient.HttpClient;
importorg.apache.commons.httpclient.HttpException;
importorg.apache.commons.httpclient.methods.PostMethod;
publicclassMobileCodeService{
publicvoidhttpGet(Stringmobile,StringuserID)throwsException
{
//http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo?mobileCode=string&userID=string
URLurl=newURL("http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo?mobileCode="+mobile+"&userID="+userID);
HttpURLConnectionconn=(HttpURLConnection)url.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
if(conn.getResponseCode()==HttpURLConnection.HTTP_OK)//200
{
InputStreamis=conn.getInputStream();
=newByteArrayOutputStream();//
byte[]buf=newbyte[1024];
intlen=-1;
while((len=is.read(buf))!=-1)
{
//獲取結果
arrayOutputStream.write(buf,0,len);
}
System.out.println("Get方式獲取的數據是:"+arrayOutputStream.toString());
arrayOutputStream.close();
is.close();
}
}
publicvoidhttpPost(Stringmobile,StringuserID)throwsHttpException,IOException
{
//訪問路徑http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo
//HttpClient訪問
HttpClienthttpClient=newHttpClient();
PostMethodpm=newPostMethod("http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo");
pm.setParameter("mobileCode",mobile);
pm.setParameter("userID",userID);
intcode=httpClient.executeMethod(pm);
System.out.println("狀態碼:"+code);
//獲取結果
Stringresult=pm.getResponseBodyAsString();
System.out.println("獲取到的數據是:"+result);
}
publicvoidSOAP()throwsException
{
HttpClientclient=newHttpClient();
PostMethodmethod=newPostMethod("http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx");
//設置訪問方法的參數
method.setRequestBody(newFileInputStream("C:\soap.xml"));
method.setRequestHeader("Content-Type","text/xml;charset=utf-8");
intcode=client.executeMethod(method);
System.out.println("狀態碼:"+code);
//獲取結果
Stringresult=method.getResponseBodyAsString();
System.out.println("獲取到的數據是:"+result);
}
publicstaticvoidmain(String[]args)throwsException{
MobileCodeServicemcs=newMobileCodeService();
mcs.httpGet("18524012513","");
//mcs.httpPost("18524012513","");
//mcs.SOAP();
}
}
❻ java實現調用webserver
一、利用jdk web服務api實現,這里使用基於 SOAP message 的 Web 服務
1.首先建立一個Web services EndPoint:
Java代碼
package Hello;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.xml.ws.Endpoint;
@WebService
public class Hello {
@WebMethod
public String hello(String name) {
return "Hello, " + name + "\n";
}
public static void main(String[] args) {
// create and publish an endpoint
Hello hello = new Hello();
Endpoint endpoint = Endpoint.publish("http://localhost:8080/hello", hello);
}
}
Java代碼
package Hello;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.xml.ws.Endpoint;
@WebService
public class Hello {
@WebMethod
public String hello(String name) {
return "Hello, " + name + "\n";
}
public static void main(String[] args) {
// create and publish an endpoint
Hello hello = new Hello();
Endpoint endpoint = Endpoint.publish("http://localhost:8080/hello", hello);
}
}
2.使用 apt 編譯 Hello.java(例:apt -d [存放編譯後的文件目錄] Hello.java ) ,會生成 jaws目錄
3.使用java Hello.Hello運行,然後將瀏覽器指向http://localhost:8080/hello?wsdl就會出現下列顯示
4.使用wsimport 生成客戶端
使用如下:wsimport -p . -keep http://localhost:8080/hello?wsdl
5.客戶端程序:
Java代碼
class HelloClient{
public static void main(String args[]) {
HelloService service = new HelloService();
Hello helloProxy = service.getHelloPort();
String hello = helloProxy.hello("你好");
System.out.println(hello);
}
}
Java代碼
class HelloClient{
public static void main(String args[]) {
HelloService service = new HelloService();
Hello helloProxy = service.getHelloPort();
String hello = helloProxy.hello("你好");
System.out.println(hello);
}
}
❼ Java調用wsdl,怎麼實現
步驟如下:
1.下載AXIS2類庫,AXIS2是目前java調用webservice的一個主要方法(由於更新較頻繁,請自行google該類庫的網址)
2.由於是第三方webservice,直接引入AXIS2的包就可以
代碼如下:
import java.rmi.RemoteException;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.ServiceException;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
public class webServiceTest {
public String invokeRemoteFuc() {
String endpoint = "http://localhost:8080/webservice/services/helloworld";
String result = "no result!";
Service service = new Service();//新建一個service
Call call;
Object[] object = new Object[1];
object[0] = "Dear I miss you";//Object是用來存儲方法的參數
try {
call = (Call) service.createCall();
call.setTargetEndpointAddress(endpoint);// 遠程調用路徑
call.setOperationName("say");// 調用的方法名
// 設置參數名:
call.addParameter("str1", // 參數名
XMLType.XSD_STRING,// 參數類型:String
ParameterMode.IN);// 參數模式:'IN' or 'OUT'
// 設置返回值類型:
call.setReturnType(XMLType.XSD_STRING);// 返回值類型:String
result = (String) call.invoke(object);// 遠程調用
} catch (ServiceException e) {
e.printStackTrace();
} catch (RemoteException e) {
e.printStackTrace();
}
return result;//返回值
}
public static void main(String[] args) {
webServiceTest t = new webServiceTest();
String result = t.invokeRemoteFuc();
System.out.println(result); //輸出
}
}
通過AXIS2封裝好的類設置URL和參數,直接調用。
❽ java cxf動態調用服務端的webservices方法
WebService實現
1、 整個項目使用CXF來實現,在實現的過程中,在MyEclipse中對CXF下lib中的所有jar文件通過引入外部包來處理。
2、 在MyEclipse6.5中可以實現伺服器端和客戶端,但是客戶端在使用wsdl進行動態調用的過程中總是報錯,最後使用MyEclipse9.0實現了進行動態調用的實現。其中發生的錯誤如下:
(1)Exception in thread "main" java.lang.LinkageError: 正在從引導類載入器載入 JAXB 2.1 API, 但此 RI (來自jar:file:/D:/CXF/lib/jaxb-impl-2.2.5.jar!/com/sun/xml/bind/v2/model/impl/ModelBuilder.class) 需要 2.2 API。請使用授權目錄機制將 jaxb-api.jar 放在引導類載入器中。(請參閱 http://java.sun.com/j2se/1.6.0/docs/guide/standards/)
解決辦法:
通過刪除引入包中的jaxb-impl-2.2.5.jar文件可以解決。
(2)java.lang.IllegalArgumentException: Can not set final com.sun.tools.internal.xjc.reader.internalizer.InternalizationLogic field
❾ java程序怎麼調用webservice介面,實現發送簡訊功能
給你一個最簡單的方法:
第一、根據http://134.224.102.6:80/CompanySendSmInf/services/SmsInf?wsdl 拿到WSDL文件。
第二、根據Axis的jar包,把文件生成客服端java代碼。(可以把java文件打成jar文件,便於管理。怎麼生成java代碼,網路里都有說明我就不寫了。)
第三、在你工程里用AXIS的功能屬性,調用外部介面;給你一個格式模板:
MobileCodeWSLocator l=new MobileCodeWSLocator();//MobileCodeWSLocator是WSDL文件生成客服端java類;
MobileCodeWSSoap s=l.getMobileCodeWSSoap();();//MobileCodeWSSoap 是WSDL文件生成客服端java類
String m=s.getMobileCodeInfo("13811534742", "");
如果你用Axis生成的java類,格式和上面一樣;自己參考一下就懂了。
你上面明顯的連接異常,第三方服務明顯沒有開,WEBSERVICE可以設置戶名、密碼,像行所有的WEBSERVICE都設置,安全考慮吧。
❿ 用java調用https webservice,該怎麼處理
publicStringsendPost(Stringurl,Stringparam){
StringrequestData=param;//參數
StringrequsetString=url;//遠程介面地址
//'tcare.
//信任任何證書
X509TrustManagertrustManager=newX509TrustManager(){
publicvoidcheckClientTrusted(X509Certificate[]chain,StringauthType)throwsCertificateException{
//Don'tdoanything.
}
publicvoidcheckServerTrusted(X509Certificate[]chain,StringauthType)throwsCertificateException{
//Don'tdoanything.
}
publicX509Certificate[]getAcceptedIssuers(){
//Don'tdoanything.
returnnull;
}
};
//把信任證書放到ssl中
SSLContextsslcontext;
Stringresult="";
try{
歷侍sslcontext=SSLContext.getInstance("SSL");
sslcontext.init(null,newTrustManager[]{trustManager},null);
//
//(
//,amethodwhichdoesn't
//existanywhereIcanfind,buthey-ho).
SSLSocketFactorysf=newSSLSocketFactory(sslcontext);
sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
DefaultHttpClienthttpclient=newDefaultHttpClient();
httpclient.getConnectionManager().getSchemeRegistry().register(newScheme("https",sf,VariableTool.HTTP_NUM));
HttpPosthttpPost=newHttpPost(requsetString);
//執行https請求
httpPost.setHeader("Authorization","basic"+"dGNsb3VkYWRtaW46dGNsb3VkMTIz");
httpPost.setHeader("Content-type","application/xml");
凳碰StringEntityreqEntity;
//將請求參數封裝成HttpEntity
reqEntity=newStringEntity(requestData,"UTF-8");
BufferedHttpEntitybhe=newBufferedHttpEntity(reqEntity);
肢粗吵httpPost.setEntity(bhe);
HttpResponseresponse=httpclient.execute(httpPost);
HttpEntityresEntity=response.getEntity();
InputStreamReaderreader=newInputStreamReader(resEntity.getContent());
char[]buff=newchar[1024];
intlength=0;
while((length=reader.read(buff))!=-1){
result+=newString(buff,0,length);
}
logger.debug("調用ws介面返回:"+result);
httpclient.getConnectionManager().shutdown();
}catch(Exceptione){
e.printStackTrace();
return"";
}
returnresult;
}
result就是遠程介面返回的介面