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();
		}
	}
}

运行的结果:


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值