package com.gatgets.http.webservice;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* socket客户端(下载网页及元素,暂不支持断点传续)
* @author 王正镇
* @date 2011-8-12
*/
public class MyClient {
private static Pattern p = null;
static {
p = Pattern.compile("http://(.+?):?(\\d+)?(/.*)?");
}
public static void getResource(String url) throws Exception {
int port; // 端口号
String host; // 主机
String resourcePath; // 请求路径
Matcher m = p.matcher(url);
if (m.matches()) {
host = m.group(1);
port = m.group(2) == null ? 80 : Integer.parseInt(m.group(2)); // 默认端口 80
resourcePath = m.group(3) == null ? "/" : m.group();
} else {
throw new Exception("请输入合法的连接地址");
}
Socket socket = new Socket(host, port);
// 使用socket get连接
OutputStream os = socket.getOutputStream();
// 请求消息头
String lineSeparator = System.getProperty("line.separator");
os.write(("GET " + resourcePath + " HTTP/1.1" + lineSeparator).getBytes());
os.write(("Host: " + host + lineSeparator).getBytes());
os.write(lineSeparator.getBytes());
// 获得返回结果
InputStream is = socket.getInputStream();
// 读取流(必须用二进制读取)
// 读取头消息(跳过)
BinaryInputStream bis = new BinaryInputStream(is);
String tmp = "";
while (!(tmp = bis.readLine()).equals("")) {
System.out.println(tmp);
}
bis.close();
// 保存(以下所有内容)
String filePath = ClassLoader.getSystemResource("").toString().substring(5); // 文件路径
String filename = filePath + UUID.randomUUID().toString(); // 产生一个随机文件名
// 如果有扩展名,则加上扩展名
int indexDoc = -1;
if ((indexDoc = resourcePath.lastIndexOf(".")) != -1) {
filename += resourcePath.substring(indexDoc);
}
OutputStream filenameOS = new FileOutputStream(filename);
copyStream(is, filenameOS);
System.out.println("----------------------" + filename + "------------------------");
os.close();
is.close();
filenameOS.close();
}
/**
* 复制流
* @throws IOException
*/
public static void copyStream(InputStream is, OutputStream os) {
byte[] b = new byte[2048]; // 缓存
int length = 0;
try {
while ((length = is.read(b)) > 0) {
os.write(b, 0, length);
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 执行入口
* @param args
*/
public static void main(String[] args) {
try {
getResource("http://www.baidu.com:80");
getResource("http://www.google.com.hk/intl/zh-CN/images/logo_cn.png");
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 不同系统分隔符不相同
* @author 王正镇
* @date 2011-8-12
*/
class BinaryInputStream {
private enum SYSTYPE{LINUX, WINDOWS};
private ByteArrayOutputStream bos = null;
private InputStream is = null;
private SYSTYPE type = null; // 系统类型
public BinaryInputStream(InputStream is) {
this.is = is;
this.bos = new ByteArrayOutputStream();
this.type = System.getProperty("line.separator").length() > 1 ? SYSTYPE.WINDOWS : SYSTYPE.LINUX;
}
/**
* 读取一行
* @return
* @throws IOException
*/
public String readLine() throws IOException {
String str = ""; // 返回结果
int b = -1;
while (is != null && (b = is.read()) != -1) {
// windows下换行符
if (b == '\r' && type == SYSTYPE.WINDOWS) {
if (is.read() == '\n') {
str = bos.toString();
bos.reset(); // 重置
break;
}
}
// linux下换行符
if (b == '\n' && type == SYSTYPE.LINUX) {
str = bos.toString();
bos.reset(); // 重置
break;
}
bos.write(b);
}
return str.trim();
}
public void close() throws IOException {
if (bos != null) {
bos.close();
}
}
}
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* socket客户端(下载网页及元素,暂不支持断点传续)
* @author 王正镇
* @date 2011-8-12
*/
public class MyClient {
private static Pattern p = null;
static {
p = Pattern.compile("http://(.+?):?(\\d+)?(/.*)?");
}
public static void getResource(String url) throws Exception {
int port; // 端口号
String host; // 主机
String resourcePath; // 请求路径
Matcher m = p.matcher(url);
if (m.matches()) {
host = m.group(1);
port = m.group(2) == null ? 80 : Integer.parseInt(m.group(2)); // 默认端口 80
resourcePath = m.group(3) == null ? "/" : m.group();
} else {
throw new Exception("请输入合法的连接地址");
}
Socket socket = new Socket(host, port);
// 使用socket get连接
OutputStream os = socket.getOutputStream();
// 请求消息头
String lineSeparator = System.getProperty("line.separator");
os.write(("GET " + resourcePath + " HTTP/1.1" + lineSeparator).getBytes());
os.write(("Host: " + host + lineSeparator).getBytes());
os.write(lineSeparator.getBytes());
// 获得返回结果
InputStream is = socket.getInputStream();
// 读取流(必须用二进制读取)
// 读取头消息(跳过)
BinaryInputStream bis = new BinaryInputStream(is);
String tmp = "";
while (!(tmp = bis.readLine()).equals("")) {
System.out.println(tmp);
}
bis.close();
// 保存(以下所有内容)
String filePath = ClassLoader.getSystemResource("").toString().substring(5); // 文件路径
String filename = filePath + UUID.randomUUID().toString(); // 产生一个随机文件名
// 如果有扩展名,则加上扩展名
int indexDoc = -1;
if ((indexDoc = resourcePath.lastIndexOf(".")) != -1) {
filename += resourcePath.substring(indexDoc);
}
OutputStream filenameOS = new FileOutputStream(filename);
copyStream(is, filenameOS);
System.out.println("----------------------" + filename + "------------------------");
os.close();
is.close();
filenameOS.close();
}
/**
* 复制流
* @throws IOException
*/
public static void copyStream(InputStream is, OutputStream os) {
byte[] b = new byte[2048]; // 缓存
int length = 0;
try {
while ((length = is.read(b)) > 0) {
os.write(b, 0, length);
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 执行入口
* @param args
*/
public static void main(String[] args) {
try {
getResource("http://www.baidu.com:80");
getResource("http://www.google.com.hk/intl/zh-CN/images/logo_cn.png");
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 不同系统分隔符不相同
* @author 王正镇
* @date 2011-8-12
*/
class BinaryInputStream {
private enum SYSTYPE{LINUX, WINDOWS};
private ByteArrayOutputStream bos = null;
private InputStream is = null;
private SYSTYPE type = null; // 系统类型
public BinaryInputStream(InputStream is) {
this.is = is;
this.bos = new ByteArrayOutputStream();
this.type = System.getProperty("line.separator").length() > 1 ? SYSTYPE.WINDOWS : SYSTYPE.LINUX;
}
/**
* 读取一行
* @return
* @throws IOException
*/
public String readLine() throws IOException {
String str = ""; // 返回结果
int b = -1;
while (is != null && (b = is.read()) != -1) {
// windows下换行符
if (b == '\r' && type == SYSTYPE.WINDOWS) {
if (is.read() == '\n') {
str = bos.toString();
bos.reset(); // 重置
break;
}
}
// linux下换行符
if (b == '\n' && type == SYSTYPE.LINUX) {
str = bos.toString();
bos.reset(); // 重置
break;
}
bos.write(b);
}
return str.trim();
}
public void close() throws IOException {
if (bos != null) {
bos.close();
}
}
}