package soapUtil;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.w3c.dom.Element;
public class UseHttpPost {
private ReadMainXml rmxMainXml;
private String Url;
private String Method;
private LinkedHashMap<String, String> Parameter;
private String SoapAction;
public UseHttpPost(String Url,String Method) throws IOException, Exception{
this.Url = Url;
this.rmxMainXml = new ReadMainXml(this.Url,Method);
this.Parameter = rmxMainXml.Parameter;
this.Method = rmxMainXml.Method;
this.SoapAction = rmxMainXml.SoapAction;
}
public String httpRequst(List<String> list) throws Exception {
String requst = null;
if(list.size() == 0 || list == null || list.size() != Parameter.size()) throw new Exception("锟斤拷獾斤拷锟斤拷锟斤拷锟絯ebService锟接口凤拷锟斤拷锟斤拷约锟斤拷锟斤拷一锟斤拷");
int i = 0;
for (Entry<String, String> entry : Parameter.entrySet()) {
String key = entry.getKey();
Parameter.put(key, list.get(i));
i++;
}
InputStream in = new CreatHttpXml(Url,Method,Parameter).XmlStream();
requst = soapService(in);
in.close();
return requst;
}
private String readSoapFile(InputStream input) throws Exception {
byte[] b = new byte[65535];
int len = 0;
int temp = 0;
while ((temp = input.read()) != -1) {
b[len] = (byte) temp;
len++;
}
String soapxml = new String(b);
// 锟芥换xml锟侥硷拷锟斤拷占位锟斤拷
return replace(soapxml);
}
private String replace(String param) {
char[] charArray = param.toCharArray();
int num = -1;
for (int i = charArray.length - 1; i >= 0; i--) {
if (charArray[i] != '\0') {
num = i;
break;
}
}
return num == -1 ? null : param.substring(0, num + 1);
}
private String soapService(InputStream input) throws Exception {
String request = "";
String soap = readSoapFile(input);
// 锟斤拷装锟斤拷锟捷o拷锟斤拷锟斤拷锟斤拷byte锟斤拷式锟斤拷锟斤拷
byte[] b = soap.getBytes();
// 锟斤拷要锟斤拷锟斤拷锟斤拷锟斤拷锟�
URL url = new URL(Url);
// 锟斤拷锟斤拷锟斤拷
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// 锟斤拷锟斤拷锟斤拷锟斤拷式锟斤拷锟斤拷息头锟皆硷拷锟斤拷时时锟斤拷
conn.setRequestMethod("POST");
conn.setConnectTimeout(5000);
// 锟斤拷锟斤拷锟角凤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
conn.setRequestProperty("Content-Length", String.valueOf(b.length));
conn.setRequestProperty("SoapAction", SoapAction);
// 取锟斤拷锟斤拷锟斤拷锟�
OutputStream outPut = conn.getOutputStream();
outPut.write(b);
outPut.flush();
outPut.close();
// 锟叫讹拷锟斤拷锟斤拷锟角凤拷晒锟�
InputStream in;
if (conn.getResponseCode() == 200)
in = conn.getInputStream();
else if (conn.getResponseCode() == 500)
in = conn.getErrorStream();
else {
System.out.print(conn.getResponseCode());
return request;
}
// byte[] b1 = new byte[65535];
// int len = 0;
// int temp = 0;
// while ((temp = in.read()) != -1) {
// b1[len] = (byte) temp;
// len++;
// }
// in.close();
ResultXml r = new ResultXml();
request = r.CreatDom(in);
return request;
}
}