JAVA -get-post-soap方式反問外部webservices
package com.handkoo.webservices;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
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 MobileCodeServices {
/**
* GET請求
*
* @param mobilecode mobilecode
* @param userID 使用者的編號
* @throws IOException io異常
*/
public void get(String mobilecode,String userID) throws IOException{
URL url = new URL("http://ws.webxml.com.cn//WebServices/MobileCodeWS.asmx/getMobileCodeInfo?mobileCode="
+mobilecode+"&userID="+userID);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
// 判斷是否請求成功
if(conn.getResponseCode()==HttpURLConnection.HTTP_OK){
// 獲取inputstream
InputStream inputStream =conn.getInputStream();
// 讀取記憶體
ByteArrayOutputStream boas = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = -1;
while((len = inputStream.read(buffer))!=-1){
boas.write(buffer, 0, len);
}
System.out.println("get請求獲取的資料:"+boas.toString("utf-8"));
boas.close();
inputStream.close();
}
}
/**
* post請求
* @param mobileCode 手機號碼
* @param userId 使用者的編號
* @throws HttpException http異常
* @throws IOException io異常
*/
public void post(String mobileCode,String userId) throws HttpException, IOException{
HttpClient httpClient = new HttpClient();
PostMethod postMethod = new PostMethod("http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo");
postMethod.setParameter("mobileCode", mobileCode);
postMethod.setParameter("userID", userId);
int code = httpClient.executeMethod(postMethod);
String result = postMethod.getResponseBodyAsString();
System.out.println("post請求的結果為:"+result);
}
/**
* saop1 訪問方式
* @throws HttpException
* @throws IOException
*/
public void soap() throws HttpException, IOException{
HttpClient client = new HttpClient();
PostMethod postMethod = new PostMethod("http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx");
postMethod.setRequestBody(new FileInputStream("d://soap.xml"));
postMethod.setRequestHeader("Content-Type","text/xml;charset=utf-8");
int code = client.executeMethod(postMethod);
System.out.println("結果碼:"+code);
String reString = postMethod.getResponseBodyAsString();
System.out.println("post請求的結果:"+reString);
}
public static void main(String[] args){
MobileCodeServices mobileCodeServices = new MobileCodeServices();
try {
mobileCodeServices.get("xxxx", "");
mobileCodeServices.post("xxxx", "");
mobileCodeServices.soap();
} catch (IOException e) {
e.printStackTrace();
}
}
}
執行的結果:
相關文章
- Java呼叫取得天氣預報WebServicesJavaWeb
- SOAP及WebServicesWeb
- java中使用URLClassLoader訪問外部jar包的java類JavaJAR
- Java中路徑反斜槓問題Java
- 請教什麼是WebServices,在Java如何實現WebJava
- 一個JAVA程式呼叫外部程式的問題!(急)Java
- c#呼叫webservicesC#Web
- 設計差異引發WebServices安全性問題Web
- WebServices in Oracle 11gWebOracle
- Java_操作外部程式Java
- Cxf - Spring整合呼叫WebServicesSpringWeb
- Java魔法堂:呼叫外部程式Java
- 面試反問問題面試
- ABAP訪問外部SQL (轉)SQL
- Cxf - Spring整合Cxf暴露WebServicesSpringWeb
- 網上常用免費WebServices集合Web
- 使用位運算、值交換等方式反轉java字串-共四種方法Java字串
- java學習中問題與解決方式Java
- outerDocument訪問外部屬性方法
- springboot 整合外部tomcat war包部署方式Spring BootTomcat
- css引入外部css檔案的方式簡單介紹CSS
- 使用jQuery和YQL,以Ajax方式載入外部內容jQuery
- java反編譯工具Java編譯
- Java-反斜槓Java
- restfus webservices獲取資料的api方法RESTWebAPI
- Java 反彙編、反編譯、volitale解讀Java編譯
- 融合外部知識的常識問答
- 使用外部表訪問監聽日誌
- Java反編譯器剖析Java編譯
- Java程式的反加密(轉)Java加密
- cxf ServerFactoryBean 生成基於soap1.2的WebServicesServerBeanWeb
- 重走JAVA之路(五):面試又被問執行緒池原理?教你如何反擊Java面試執行緒
- phpmyadmin設定root賬戶外部訪問PHP
- 使用外部表訪問警告日誌檔案
- OpenStack外部網路不能訪問內部VM
- 關於excel引用外部excel的取值問題Excel
- java啟動方式Java
- 用最簡單的方式理解 IoC 控制反轉