用java http post類比soapUI調用webservice

來源:互聯網
上載者:User

標籤:webservice   java   soapui   

工作中需要用java調用peoplesoft提供的webservice介面,但peoplesoft的實施方沒有提供java調用的案例代碼,soapUI可以調用,但java代碼一直寫不出來,自行學習並調通了用java http post類比的方法,感謝天涯的zhouyun0243和Google!


import java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.HttpURLConnection;import java.net.URL;//需要commons-io的jar包import org.apache.commons.io.IOUtils;public class testwebservice {public static void main(String[] args) {try {soapSpecialConnection();} catch (Exception e) {e.printStackTrace();}}public static void soapSpecialConnection() throws Exception{String s = new String();StringBuilder soapHeader = new StringBuilder();//soapUI自動產生的request xml路徑,寫入傳入參數File file = new File("d:\\1.xml");BufferedReader reader = null;try {reader = new BufferedReader(new FileReader(file));String tempString = null;// 一次讀入一行,直到讀入null為檔案結束while ((tempString = reader.readLine()) != null) {soapHeader.append(tempString);}reader.close();} catch (IOException e) {e.printStackTrace();} finally {if (reader != null) {try {reader.close();} catch (IOException e1) {}}}System.out.println("soapHeader="+soapHeader);//設定soap請求報文的相關屬性//url從soapUI的request1的RAW標籤的POST擷取,url中不要有空格String url="http://10.60.217.86:9527/dji-hrService/services/HrService.HrServiceHttpSoap11Endpoint/HTTP/1.1";URL u = new URL(url);HttpURLConnection conn = (HttpURLConnection) u.openConnection();conn.setDoInput(true);conn.setDoOutput(true);conn.setUseCaches(false);conn.setDefaultUseCaches(false);//Host,Content-Type,SOAPAction從soapUI的request1的RAW標籤的Host,Content-Typ,SOAPActione擷取conn.setRequestProperty("Host", "10.60.217.86:9527");conn.setRequestProperty("Content-Type", "ext/xml;charset=UTF-8");conn.setRequestProperty("Content-Length", String.valueOf(soapHeader.length()));conn.setRequestProperty("SOAPAction", "urn:getWorkAttendanceByUidAndDate");conn.setRequestMethod("POST");//定義輸出資料流OutputStream output = conn.getOutputStream();if (null != soapHeader) {byte[] b = soapHeader.toString().getBytes("utf-8");//發送soap請求報文output.write(b, 0, b.length);}output.flush();output.close();//定義輸入資料流,擷取soap響應報文InputStream input = conn.getInputStream();//需設定編碼格式,否則會亂碼s=IOUtils.toString(input, "UTF-8");input.close();System.out.println("輸出的xml="+s);}}
soapUI的request1的RAW標籤示意



用java http post類比soapUI調用webservice

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.