java 调用.net web service

13 篇文章 0 订阅
/**
 * 
 */
/**
 * @author admin
 *
 */
package net.aykj.axcf.servlet;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.Hashtable;
import java.util.Iterator;

/** 
 *  
 * <p>Title: HttpClient.java</p> 
 * <p>Description:HttpURLConnection调用方式</p> 
 */  
public class HttpClient {  
	
	/**
     * 禁止实例化
     */
    private HttpClient() {
        throw new IllegalStateException("工具类禁止实例化");
    }

    public static String executeMethod(Hashtable<?, ?> ht,String strUrl,String namespace) {
		String RESULT="";
		String SOAPAction = ht.get("Method").toString();
		Iterator<String> itr = (Iterator<String>) ht.keySet().iterator(); 
		String parameters = "";
		for (int i = 0; i < ht.size(); i++) {
			String key = (String)itr.next();
			if(!"Method".equals(key)){
				parameters = parameters + "<" + key + ">" + ht.get(key) + "</" + key + ">";
			}
		}
		String soap = "<?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:Body>" + "<" + SOAPAction + " xmlns=\""+namespace+"\">" + parameters + " </" + SOAPAction
				+ ">" + " </soap:Body>" + "</soap:Envelope>";
		try {
			URL url = new URL(strUrl);
			URLConnection conn = url.openConnection();
			conn.setUseCaches(false);
			conn.setDoInput(true);
			conn.setDoOutput(true);
			conn.setConnectTimeout(5000);
			conn.setReadTimeout(8000);
			conn.setRequestProperty("Content-Length", String.valueOf(soap.getBytes().length));
			conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
			conn.setRequestProperty("SOAPAction", namespace + SOAPAction);
			OutputStream os = conn.getOutputStream();
			OutputStreamWriter osw = new OutputStreamWriter(os, "utf-8");
			osw.write(soap);
			osw.flush();
			osw.close();

			
			InputStream is = conn.getInputStream();
			RESULT = toString(is);
			
		} catch (Exception e) {
			
		
			e.printStackTrace();
		}
		return RESULT;
	}

	public static String toString(InputStream is) {

		BufferedReader reader = new BufferedReader(new InputStreamReader(is));
		StringBuilder sb = new StringBuilder();
		String line = null;

		try {
			while ((line = reader.readLine()) != null) {
				sb.append(line + "/n");
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return sb.toString();
	}
	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Hashtable<String, Object> ht = new Hashtable<String, Object>();
		ht.put("UserId", "456");
		ht.put("Time", "2017/12/28");
		ht.put("T_Withdrawals", 1000);
		ht.put("Method", "SetJ_Overview");
		System.out.println(executeMethod(ht,"http://192.168.1.193:8081/WebService.asmx","http://tempuri.org/"));//http://tempuri.org/
	}
    
    
}  

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值