当前位置: 技术问答>java相关
用socket建立http连接的一段实例程序,有点毛病,大家过来看看啊!!!
来源: 互联网 发布时间:2015-02-01
本文导语: import java.net.*; import java.io.*; public class MyURLConnection extends URLConnection { // timeout is measured in milliseconds, default is infinite private int timeout = 0; private InputStream in = null; private OutputStream out ...
import java.net.*;
import java.io.*;
public class MyURLConnection extends URLConnection
{
// timeout is measured in milliseconds, default is infinite
private int timeout = 0;
private InputStream in = null;
private OutputStream out = null;
private Socket soc = null;
// HTTP only
private String [] httpHeader =
{
"GET / HTTP/1.1",
"User-Agent: Java1.3.0",
"Host: ",
"Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2",
"Connection: keep-alive"
};
public MyURLConnection(URL url, int timeout)
{
super(url);
this.timeout = timeout;
httpHeader[2] += url.getHost(); // HTTP only
}
public void connect() throws IOException
{
soc = new Socket(url.getHost(), 80);
soc.setSoTimeout(timeout);
in = soc.getInputStream();
out = soc.getOutputStream();
DataOutputStream dos = new DataOutputStream(out);
// Http only
for (int i=0; i
import java.io.*;
public class MyURLConnection extends URLConnection
{
// timeout is measured in milliseconds, default is infinite
private int timeout = 0;
private InputStream in = null;
private OutputStream out = null;
private Socket soc = null;
// HTTP only
private String [] httpHeader =
{
"GET / HTTP/1.1",
"User-Agent: Java1.3.0",
"Host: ",
"Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2",
"Connection: keep-alive"
};
public MyURLConnection(URL url, int timeout)
{
super(url);
this.timeout = timeout;
httpHeader[2] += url.getHost(); // HTTP only
}
public void connect() throws IOException
{
soc = new Socket(url.getHost(), 80);
soc.setSoTimeout(timeout);
in = soc.getInputStream();
out = soc.getOutputStream();
DataOutputStream dos = new DataOutputStream(out);
// Http only
for (int i=0; i