package net.leadtour.util;
import java.io.*;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.zip.GZIPOutputStream;
public class MNetUtil {
public static String getUrlContent(String destURL) {
return getUrlContent(destURL, "");
}
public static String getUrlContent(String destURL, String paramStr) {
return getUrlContent(destURL, paramStr, 60);
}
public static String getUrlContent(String destURL, String paramStr, int timeout) {
return getUrlContent(destURL, paramStr, timeout, "UTF-8");
}
public static String getUrlContent(String destURL, String paramStr, int timeout, String encoding) {
return getUrlContent(destURL, paramStr, timeout, encoding, "POST");
}
public static String getUrlContent(String destURL, String paramStr, int timeout, String encoding, String method) {
return getUrlContent(destURL, paramStr, timeout, encoding, method, new Hashtable());
}
public static String getUrlContent(String destURL, String paramStr, int timeout, String encoding, String method, Hashtable head) {
HttpURLConnection httpConn = null;
OutputStream out = null;
BufferedReader reader = null;
try {
if (destURL.contains("https:")) {
SslConnection urlConnect = new SslConnection();
httpConn = urlConnect.openConnection(destURL);
} else {
URL url = new URL(destURL);
httpConn = (HttpURLConnection) url.openConnection();
}
HttpURLConnection.setFollowRedirects(true);
httpConn.setRequestProperty("content-type", "text/html");
Enumeration en = head.keys();
while (en.hasMoreElements()) {
String ok = en.nextElement().toString();
httpConn.setRequestProperty(ok, head.get(ok).toString());
}
httpConn.setRequestMethod(method);
httpConn.setConnectTimeout(timeout * 1000);
httpConn.setReadTimeout(timeout * 1000);
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
if ("POST".equals(method)) {
out = httpConn.getOutputStream();
out.write(paramStr.getBytes(encoding));
out.flush();
out.close();
}
// httpConn.connect();
StringBuffer buf = new StringBuffer();
if (httpConn.getResponseCode() == 500) {
reader = new BufferedReader(new InputStreamReader(httpConn.getErrorStream(), encoding));
String line;
while ((line = reader.readLine()) != null) {
buf.append(line);
buf.append("\r\n");
}
} else {
reader = new BufferedReader(new InputStreamReader(httpConn.getInputStream(), encoding));
String line;
while ((line = reader.readLine()) != null) {
buf.append(line);
buf.append("\r\n");
}
}
return buf.toString();
} catch (Exception ex) {
return "<Error>" + ex.toString() + "</Error>";
} finally {
try {
httpConn.disconnect();
} catch (Exception e) {
}
}
}
public static String getGZIPUrlContent(String destURL, String paramStr, int timeout, String encoding, String method, Hashtable head) {
HttpURLConnection httpConn = null;
GZIPOutputStream out = null;
BufferedReader reader = null;
try {
URL url = new URL(destURL);
httpConn = (HttpURLConnection) url.openConnection();
HttpURLConnection.setFollowRedirects(true);
httpConn.setRequestProperty("content-type", "text/html");
Enumeration en = head.keys();
while (en.hasMoreElements()) {
String ok = en.nextElement().toString();
httpConn.setRequestProperty(ok, head.get(ok).toString());//)
}
httpConn.setRequestMethod(method);
httpConn.setConnectTimeout(timeout * 1000);
httpConn.setReadTimeout(timeout * 1000);
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
if ("POST".equals(method)) {
out = new GZIPOutputStream(httpConn.getOutputStream());
out.write(paramStr.getBytes(encoding));
out.flush();
out.close();
}
// httpConn.connect();
StringBuffer buf = new StringBuffer();
if (httpConn.getResponseCode() == 500) {
reader = new BufferedReader(new InputStreamReader(httpConn.getErrorStream(), encoding));
String line;
while ((line = reader.readLine()) != null) {
buf.append(line);
buf.append("\r\n");
}
} else {
reader = new BufferedReader(new InputStreamReader(httpConn.getInputStream(), encoding));
String line;
while ((line = reader.readLine()) != null) {
buf.append(line);
buf.append("\r\n");
}
}
return buf.toString();
} catch (IOException ex) {
return "<Error>" + ex.toString() + "</Error>";
} finally {
try {
httpConn.disconnect();
} catch (Exception e) {
}
}
}
public static String getUrlXml(String str_url, String string) {
HttpURLConnection httpConn = null;
PrintWriter out = null;
BufferedReader reader = null;
try {
URL url = new URL(str_url);
httpConn = (HttpURLConnection) url.openConnection();
HttpURLConnection.setFollowRedirects(true);
httpConn.setRequestProperty("content-type", "text/xml");
httpConn.setRequestMethod("POST");
httpConn.setConnectTimeout(450000);
httpConn.setReadTimeout(450000);
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
out = new PrintWriter(httpConn.getOutputStream());
out.print(string);
out.flush();
httpConn.connect();
reader = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
StringBuffer buf = new StringBuffer();
String line;
while ((line = reader.readLine()) != null) {
buf.append(line);
buf.append("\r\n");
}
return buf.toString();
} catch (IOException ex) {
return "<Error>" + ex.toString() + "</Error>";
}
}
public static String SoapRequest(String callurl, String soapAction, String xml) throws Exception {
if (xml == null) throw new Exception("");
if (callurl == null) throw new Exception("");
// StringReader reader = null;
String result = "";
// System.out.println("UrlAddress:" + callurl);
// System.out.println("SoapAction:" + soapAction);
URL u0 = new URL(callurl);
HttpURLConnection conn = (HttpURLConnection) u0.openConnection();
byte[] contentbyte = xml.getBytes();
conn.setRequestMethod("POST");
conn.setRequestProperty("SOAPAction", soapAction);
conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
conn.setRequestProperty("Content-Length", Integer.toString(contentbyte.length));
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
OutputStream out = conn.getOutputStream();
out.write(contentbyte);
out.flush();
out.close();
InputStream in = conn.getInputStream();
StringBuffer buffer = new StringBuffer();
int i = 0;
while (i != -1) {
i = in.read();
if (i != -1) {
buffer.append((char) i);
}
}
in.close();
if (conn.getResponseCode() != 200) {
try {
System.in.read();
} catch (Exception e) {
e.printStackTrace();
}
} else {
}
if (conn.getResponseCode() != 200) {
return null;
}
// FileOper fileOper = new FileOper();
// fileOper.writeInfoToFile(buffer.toString());
result = buffer.toString();
try {
result = new String(buffer.toString().getBytes("iso-8859-1"), "utf-8");
} catch (Exception ex) {
result = ex.getMessage();
}
return result;
}
}