当前位置: 技术问答>linux和unix
如何用socket从internet上提取图像(使用http协议)
来源: 互联网 发布时间:2015-06-22
本文导语: 我用socket写了一个程序,根据url下载网页的源文件,基本过程就是: socket() -> connect() -> send() -> recv() 现在能下载text/html类型文件,但是下载image就不行,文件内容都改变了, 恳求高人指点!谢谢! 我发送的reque...
我用socket写了一个程序,根据url下载网页的源文件,基本过程就是:
socket() -> connect() -> send() -> recv()
现在能下载text/html类型文件,但是下载image就不行,文件内容都改变了,
恳求高人指点!谢谢!
我发送的request为:
GET url HTTP/1.0
Host: www.abc.com
Accept: */*
Accept-Language: en-us
Content-Type: application/x-www-form-urlencoded
socket() -> connect() -> send() -> recv()
现在能下载text/html类型文件,但是下载image就不行,文件内容都改变了,
恳求高人指点!谢谢!
我发送的request为:
GET url HTTP/1.0
Host: www.abc.com
Accept: */*
Accept-Language: en-us
Content-Type: application/x-www-form-urlencoded
|
要下载图像等文件,是不是还得再根据图像路径发一次连接请求阿,
比如:img src=/home/images/home01.gif>
比如:img src=/home/images/home01.gif>
|
看看这个:
public boolean run() {
try {
socket = factory.createSocket(https_URL,Integer.parseInt(https_PORT));
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
InputStream inStream = socket.getInputStream();
InputStreamReader inStreamReader = new InputStreamReader(inStream);
BufferedReader in = new BufferedReader(inStreamReader);
String strGet = "GET " + sSiteURL + " HTTP/1.0nn";
out.write(strGet);
out.flush();
String line = "";
line = in.readLine();
if ("HTTP/1.0 200 OK".equals(line) || "HTTP/1.1 200 OK".equals(line)) {
for (int i = 1; i 0) {
byte[] d = new byte[2048]; // 1400
numRead = inStream.read(d);
if (numRead > 0)
oSavedFileO.write(d, 0, numRead);
else
break;
}
oSavedFileO.flush();
oSavedFileO.close();
break;
}
}
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
public boolean run() {
try {
socket = factory.createSocket(https_URL,Integer.parseInt(https_PORT));
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
InputStream inStream = socket.getInputStream();
InputStreamReader inStreamReader = new InputStreamReader(inStream);
BufferedReader in = new BufferedReader(inStreamReader);
String strGet = "GET " + sSiteURL + " HTTP/1.0nn";
out.write(strGet);
out.flush();
String line = "";
line = in.readLine();
if ("HTTP/1.0 200 OK".equals(line) || "HTTP/1.1 200 OK".equals(line)) {
for (int i = 1; i 0) {
byte[] d = new byte[2048]; // 1400
numRead = inStream.read(d);
if (numRead > 0)
oSavedFileO.write(d, 0, numRead);
else
break;
}
oSavedFileO.flush();
oSavedFileO.close();
break;
}
}
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}