URLHelp网络请求工具类。

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;



public class URLHelp {
	
	private URL url;
	private String params;
	private int timeOut;
	
	public void setTimeOut(int timeOut) {
		this.timeOut = timeOut;
	}

	public void setUrl(String path) {
		try {
			this.url = new URL(path);
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public String getParams() {
		return params;
	}
	public void setParams(String params){
		this.params=params;
	}
	public void setParams(String...params) {
		StringBuilder sb=new StringBuilder();
		for (String param : params) {
			sb.append(param);
			sb.append("&");
		}
		sb.delete(sb.length()-1, sb.length());
		this.params = sb.toString();
	}

	public URL getUrl() {
		return url;
	}

	public String doPost() throws IOException{
		URLConnection urlConnection=url.openConnection();
		HttpURLConnection httpConnection=(HttpURLConnection)urlConnection;
		httpConnection.setRequestMethod("POST");
		httpConnection.setDoInput (true);  //默认为true 所以不设置也可以
		httpConnection.setDoOutput(true);  //默认为false 发送post请求必须设置setDoOutput(true)
		httpConnection.setUseCaches(false); //是否可以使用缓存 不使用缓存
		httpConnection.setConnectTimeout(timeOut);//请求超时时间
		 //设置通用的请求属性 消息报头 即设置头字段 更多的头字段信息可以查阅HTTP协议
		httpConnection.setRequestProperty("accept", "*/*");
		httpConnection.setRequestProperty("connection", "Keep-Alive");
	//	httpConnection.setRequestProperty("Content-Type", "text/javascript; charset=UTF-8");
		
		PrintWriter pw=new PrintWriter(new OutputStreamWriter(httpConnection.getOutputStream(),"UTF-8"));
		//log.error("参数:"+this.params);
		pw.write(this.params);
		pw.flush();
		pw.close();
		httpConnection.connect();
		//log.error("连接地址:"+httpConnection.getURL().getHost());
		StringBuilder sb=new StringBuilder();
		if(httpConnection.getResponseCode()==HttpURLConnection.HTTP_OK){
			BufferedReader br=new BufferedReader(new InputStreamReader(httpConnection.getInputStream(),"UTF-8"));
			String line="";
			while((line=br.readLine())!=null){
				sb.append(line);
			}
			br.close();
			httpConnection.disconnect();
		}else{
			BufferedReader br=new BufferedReader(new InputStreamReader(httpConnection.getErrorStream(),"UTF-8"));
			String line="";
			while((line=br.readLine())!=null){
				sb.append(line);
			}
			br.close();
			httpConnection.disconnect();
		}
		
		return sb.toString();
	}
	/**
	 * SOAP协议1.1
	 * */
	public String soap(String soapActionString) throws IOException{
		soapActionString=soapActionString==null?"":soapActionString;
		URLConnection urlConnection=url.openConnection();
		HttpURLConnection httpConnection=(HttpURLConnection)urlConnection;
		httpConnection.setRequestMethod("POST");
		httpConnection.setDoInput (true);  //默认为true 所以不设置也可以
		httpConnection.setDoOutput(true);  //默认为false 发送post请求必须设置setDoOutput(true)
		httpConnection.setUseCaches(false); //是否可以使用缓存 
		httpConnection.setConnectTimeout(timeOut);//请求超时时间
		//httpConnection.setRequestProperty("Host", "www.webxml.com.cn");
		httpConnection.setRequestProperty("Content-Length", String.valueOf(this.params.length()));
		//httpConnection.setRequestProperty("Content-Type", "application/soap+xml; charset=utf-8");
		httpConnection.setRequestProperty("Content-Type", "text/xml;charset=utf-8");
		httpConnection.setRequestProperty("SOAPAction", soapActionString);

		PrintWriter pw=new PrintWriter(new OutputStreamWriter(httpConnection.getOutputStream(),"UTF-8"));
		//log.error("参数:"+this.params);
		pw.write(this.params);
		pw.flush();
		pw.close();
		httpConnection.connect();
		StringBuilder sb=new StringBuilder();
		if(httpConnection.getResponseCode()==HttpURLConnection.HTTP_OK){
			BufferedReader br=new BufferedReader(new InputStreamReader(httpConnection.getInputStream(),"UTF-8"));
			String line="";
			while((line=br.readLine())!=null){
				sb.append(line);
			}
			br.close();
			httpConnection.disconnect();
		}else{
			BufferedReader br=new BufferedReader(new InputStreamReader(httpConnection.getErrorStream(),"UTF-8"));
			String line="";
			while((line=br.readLine())!=null){
				sb.append(line);
			}
			br.close();
			httpConnection.disconnect();
		}
		return sb.toString();
	
	}
	public InputStream soapGetInputStream(String soapActionString) throws IOException{
		soapActionString=soapActionString==null?"":soapActionString;
		URLConnection urlConnection=url.openConnection();
		HttpURLConnection httpConnection=(HttpURLConnection)urlConnection;
		httpConnection.setRequestMethod("POST");
		httpConnection.setDoInput (true);  //默认为true 所以不设置也可以
		httpConnection.setDoOutput(true);  //默认为false 发送post请求必须设置setDoOutput(true)
		httpConnection.setUseCaches(false); //是否可以使用缓存 
		httpConnection.setConnectTimeout(timeOut);//请求超时时间
		//httpConnection.setRequestProperty("Host", "www.webxml.com.cn");
		httpConnection.setRequestProperty("Content-Length", String.valueOf(this.params.length()));
		//httpConnection.setRequestProperty("Content-Type", "application/soap+xml; charset=utf-8");
		httpConnection.setRequestProperty("Content-Type", "text/xml;charset=utf-8");
		httpConnection.setRequestProperty("SOAPAction", soapActionString);
		
		PrintWriter pw=new PrintWriter(new OutputStreamWriter(httpConnection.getOutputStream(),"UTF-8"));
		//log.error("参数:"+this.params);
		pw.write(this.params);
		pw.flush();
		pw.close();
		httpConnection.connect();
		InputStream inputStream=null;
		if(httpConnection.getResponseCode()==HttpURLConnection.HTTP_OK){
		   inputStream=new BufferedInputStream(httpConnection.getInputStream());
		  // OutputStream outputStream=new ByteArrayOutputStream();
		   byte[] buffer=new byte[inputStream.available()];
		   inputStream.read(buffer);
		  // outputStream.write(buffer);
		   inputStream=new ByteArrayInputStream(buffer);
			httpConnection.disconnect();
		}else{
			StringBuilder sb=new StringBuilder();
			BufferedReader br=new BufferedReader(new InputStreamReader(httpConnection.getErrorStream(),"UTF-8"));
			String line="";
			while((line=br.readLine())!=null){
				sb.append(line);
			}
			br.close();
			httpConnection.disconnect();
		}
		return inputStream;
	
	}
	
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值