Use Webservice Through Primitive XML HTTP Requestion

I want to find a 3-party accessing web service lib  which using in a little sentence.

But the web service defindes it's custom AuthHeader object type. For passing the authentication info, it's not easy to find a path.

At the same time, I just want to test whether the web service is running correctly. Beacuse I use ksoap2-android lib to access it and it's hard to analyse the response...

Despite use 3-party lib to access web service, I just create a http requestion which accords with the SOAP requestion content.

The whole cords:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;


public class SynDataFromWebservice {

	public static void main(String[] args) {
		
		String responseString = "";
		String outputString = "";
		String wsUrString = "http://192.168.1.11/GetDataService.asmx";
		String METHODNAME = "GetDataSet";
		String nameSpaceString = "http://www.domain.com/";
		String soapActionString = "\"" + nameSpaceString+METHODNAME +"\"";
		String oldTimeString = "1970-01-01T00:00:00.000Z";
		URL url = null;
		try {
			url = new URL(wsUrString);
			
		} catch (MalformedURLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		HttpURLConnection connection = null;
		try {
			connection = (HttpURLConnection)url.openConnection();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		ByteArrayOutputStream bout = new ByteArrayOutputStream();
		String xmlInputString = 
				"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
				+"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
				+" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" "
				+" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
				+" <soap:Header>"
				+"   <MyAuthHeader xmlns=\""+nameSpaceString+"\">"
				+"     <passWord>password</passWord>"
				+"   </MyAuthHeader>"
				+" </soap:Header>"
				+"  <soap:Body>"
				+"     <"+METHODNAME+" xmlns=\""+nameSpaceString+"\">"
				+"        <oldTime>"+ oldTimeString +"</oldTime>"
				+"     </"+ METHODNAME +">"
				+"  </soap:Body>"
				+"</soap:Envelope>";
		byte[] buffer = new byte[xmlInputString.length()];
		buffer = xmlInputString.getBytes();
		try {
			bout.write(buffer);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		byte[] b = bout.toByteArray();
		
		connection.setRequestProperty("Host", "localhost");
		connection.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
		connection.setRequestProperty("Content-Length", String.valueOf(b.length));
		connection.setRequestProperty("SOAPAction", soapActionString);
		try {
			connection.setRequestMethod("POST");
		} catch (ProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		connection.setDoOutput(true);
		connection.setDoInput(true);
		OutputStream outputStream = null;
		try {
			outputStream = connection.getOutputStream();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			outputStream.write(b);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			outputStream.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			InputStreamReader reader = new InputStreamReader(connection.getInputStream(), "utf-8");
			BufferedReader in = new BufferedReader(reader);
			while((responseString = in.readLine()) != null){
				outputString += responseString;
			}			
			File saveFile = new File("e:\\data.xml");
			if(saveFile.exists()){
				saveFile.delete();
			}else{
				saveFile.createNewFile();
			}
			FileOutputStream fileOutputStream = new FileOutputStream(saveFile);
			Writer writer = new BufferedWriter(new OutputStreamWriter(fileOutputStream, "utf-8"));
			writer.write(outputString);
			//fileOutputStream.write(outputString.getBytes("UTF-8"));
			fileOutputStream.close();
			writer.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}



Links:

1.http://technology.amis.nl/2011/06/29/how-to-call-a-call-a-webservice-directly-from-java-without-webservice-library/;


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值