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