/**
* 2013-2-21
*
* Copyright (C) 2012 Ignite Vision Ltd.
*
* WARNING:
* This code is the sole property of Ignite Vision Limited, and is protected
* by international copyright laws.
* This program is confidential and proprietary, not to be disclosed
* without written authorization from Ignite Vision Limited.
* Any use, duplication, or disclosure of this program by other than
* Ignite Vision Limited strictly forbidden by law.
*/
package com.yulon.util;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
/**
* @author YLPan
* @date 2013-2-21
*
*/
public final class NetTool {
public final static boolean DEBUG = true;//是否输出调试信息
private static int BUFFER_SIZE = 8096;//缓冲区大小
/**
* conn.setRequestProperty("User-agent","Mozilla/5.0 (Linux; Android 4.2.1; Nexus 7 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Safari/535.19");
* @author panyl
* @description
* @date 2013-3-9
* @param urlpath
* @param encoding
* @return
* @throws Exception
*/
public static String getTextContent(String urlpath, String encoding)
throws Exception {
URL url = new URL(urlpath);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(6 * 1000);// 设置超时时间
if (conn.getResponseCode() == 200) {
InputStream inStream = conn.getInputStream();
byte[] data = readStream(inStream);
conn.disconnect();
return new String(data, encoding);
}
conn.disconnect();
return null;
}
public static String getTextContent(String urlpath, Map<String, String> parmas,String encoding)
throws Exception {
StringBuilder sb = new StringBuilder();
for (Map.Entry<String, String> entry : parmas.entrySet()) {
sb.append(entry.getKey()).append("=").append(
URLEncoder.encode(entry.getValue(), encoding));
sb.append("&");
}
if (sb.indexOf("&") != -1)
sb.deleteCharAt(sb.length() - 1);
urlpath+="?"+sb.toString();
System.out.println("urlpath:"+urlpath);
URL url = new URL(urlpath);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(6 * 1000);// 设置超时时间
System.out.println("conn.getResponseCode():"+conn.getResponseCode());
if (conn.getResponseCode() == 200) {
InputStream inStream = conn.getInputStream();
byte[] data = readStream(inStream);
conn.disconnect();
return new String(data, encoding);
}
conn.disconnect();
return null;
}
public static String getTextContent(String urlpath,String cookies, String encoding)
throws Exception {
URL url = new URL(urlpath);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(6 * 1000);// 设置超时时间
if(cookies!=null&&!"".equals(cookies)){
conn.setRequestProperty("Cookie", cookies);
}
if (conn.getResponseCode() == 200) {
InputStream inStream = conn.getInputStream();
byte[] data = readStream(inStream);
return new String(data, encoding);
}
return null;
}
public static byte[] getByteData(String urlpath, String encoding)
throws Exception {
URL url = new URL(urlpath);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(6 * 1000);// 设置超时时间
if (conn.getResponseCode() == 200) {
InputStream inStream = conn.getInputStream();
byte[] data = readStream(inStream);
return data;
}
return null;
}
/**
* 将HTTP资源另存为文件
*
* @param destUrl String
* @param fileName String
* @throws Exception
*/
public static void saveToFile(String destUrl, String fileName) throws IOException {
FileOutputStream fos = null;
BufferedInputStream bis = null;
HttpURLConnection httpUrl = null;
URL url = null;
byte[] buf = new byte[BUFFER_SIZE];
int size = 0;
//建立链接
url = new URL(destUrl);
httpUrl = (HttpURLConnection) url.openConnection();
//连接指定的资源
httpUrl.connect();
//获取网络输入流
bis = new BufferedInputStream(httpUrl.getInputStream());
//建立文件
File file = new File(fileName);
if(!file.exists()) {
if(!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
file.createNewFile();
}
fos = new FileOutputStream(fileName);
if (DEBUG)
System.out.println("正在获取链接[" + destUrl + "]的内容...\n将其保存为文件["
+ fileName + "]");
//保存文件
while ((size = bis.read(buf)) != -1)
fos.write(buf, 0, size);
fos.close();
bis.close();
httpUrl.disconnect();
}
public static byte[] readStream(InputStream inStream) throws Exception {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
byte[] data = outStream.toByteArray();// 二进制数据
outStream.close();
inStream.close();
return data;
}
/**
*
* @param urlpath
* @param parmas
* @param encoding
* @return
* @throws Exception
*/
public static InputStream getContent(String urlpath,
Map<String, String> parmas, String encoding) throws Exception {
StringBuilder sb = new StringBuilder();
for (Map.Entry<String, String> entry : parmas.entrySet()) {
sb.append(entry.getKey()).append("=").append(
URLEncoder.encode(entry.getValue(), encoding));
sb.append("&");
}
if (sb.indexOf("&") != -1)
sb.deleteCharAt(sb.length() - 1);
byte[] data = sb.toString().getBytes();
URL url = new URL(urlpath);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Charset", "UTF-8");
connection.setRequestProperty("Content-Length", String
.valueOf(data.length));
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
connection.setDoOutput(true);
connection.setDoInput(true);
DataOutputStream dataOutputStream = new DataOutputStream(connection
.getOutputStream());
dataOutputStream.write(data);
dataOutputStream.flush();
dataOutputStream.close();
// int responseCode=sendContent(urlpath, parmas, encoding);
// if (responseCode == 200) {
// return connection.getInputStream();
// }
InputStream inputStream=connection.getInputStream();
//connection.disconnect();
return inputStream;
}
public static String login(String loginUrl,Map<String,String> parmas,String encoding) throws Exception {
StringBuilder cookies=new StringBuilder();
StringBuilder sb = new StringBuilder();
for (Map.Entry<String, String> entry : parmas.entrySet()) {
sb.append(entry.getKey()).append("=").append(
URLEncoder.encode(entry.getValue(), encoding));
sb.append("&");
}
if (sb.indexOf("&") != -1)
sb.deleteCharAt(sb.length() - 1);
byte[] data = sb.toString().getBytes();
URL url = new URL(loginUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("User-Agent", "Mozilla/5.0");
//connection.setRequestProperty("referer", "http://reg