Java工作笔记-发送SOAP协议请求
生活随笔
收集整理的這篇文章主要介紹了
Java工作笔记-发送SOAP协议请求
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
這里搭建WebService采用JDKService那種最簡單的方式,在此博文中不再說明。
以前說過,調用javaw的API封包是這樣的:
這里模擬下。
客戶端結構如下:
源碼如下:
Main2.java
package webservice.client;import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import java.util.Scanner;public class Main2 {public static void main(String[] args) throws IOException {URL url = new URL("http://localhost:9998/weatherService?wsdl");HttpURLConnection connection = (HttpURLConnection) url.openConnection();connection.setRequestMethod("POST");connection.setRequestProperty("Content-Type", "text/xml; charset=utf-8");//打開通信connection.setDoInput(true);connection.setDoOutput(true);//拼接符合協議需求的數據格式String info = buildXML("南京");connection.getOutputStream().write(info.getBytes());int responseCode = connection.getResponseCode();if(responseCode == 200){//正常InputStream inputStream = connection.getInputStream();Scanner sc = new Scanner(inputStream);while(sc.hasNext()){System.out.println(sc.nextLine());}sc.close();}else{System.out.println(responseCode);}System.out.println("over");}private static String buildXML(String cityName){String str = "<?xml version=\"1.0\" ?><S:Envelope xmlns:S=\"http://schemas.xmlsoap.org/soap/envelope/\"><S:Body><ns2:query xmlns:ns2=\"http://webservice/\"><arg0>";str += cityName;str += "</arg0></ns2:query></S:Body></S:Envelope>";return str;} }?
總結
以上是生活随笔為你收集整理的Java工作笔记-发送SOAP协议请求的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java笔记-Spring Boot使用
- 下一篇: Spring Boot笔记-新增嵌入式t