81,092
社区成员
发帖
与我相关
我的任务
分享
/**
* @version 1.00 2001-11-01
* @author fightboy
*/
import java.io.*;
import java.net.*;
public class SocketTest
{
public static void main(String[] args)
{
long before;
long after;
long len = 0;
before=System.currentTimeMillis();
try
{
Socket s = new Socket("101.127.88.66",8080);
//代理的地址
BufferedReader in = new BufferedReader
(new InputStreamReader(s.getInputStream()));
PrintStream out = new PrintStream(s.getOutputStream(),true);
boolean more = true;
out.println("GET "+args[0]+" HTTP/1.0");
//注1:http/1.1会有点小问题,就是用了keep-alive之后,数人
//流不会返回null,使得下边循环无法结束
//注2:使用方法如例子:java SocketTest
//http://www.sina.com.cn/
out.println("Accept-Language: zh-cn");
out.println("User-Agent: Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
out.println("Host: 202.bbb.mmm.nnn");
//把这句话中冒号后的内容改为你自己的ip就可以了
out.println();
out.println();
while (more)
{
String line = in.readLine();
if (line == null) more = false;
else
{
//System.out.println(line);
len = len + line.length();
}
}
after=System.currentTimeMillis();
System.out.println("time cost: "+(after-before));
System.out.println("bytes get: "+len);
}
catch (IOException e)
{
System.out.println("Error" + e);
}
}
}
方法2是很简单的方法:
完成同样的功能:
import java.util.Properties;
import java.net.*;
import java.io.*;
public class TestHttpProxy {
public static void main(String[] args) {
String sUrl = "http://java.sun.com/index.html";
Properties prop = System.getProperties();
prop.put("http.proxyHost","192.168.1.111");
prop.put("http.proxyPort","80");
try{
URL su = new URL(sUrl);
System.out.println("url : " + su);
URLConnection uc = su.openConnection();
System.out.println("uc : " + uc);
InputStream is = su.openStream();
System.out.println("ic : " + is.read());
is.close();
System.out.println("ok");
} catch(Exception e) {
e.printStackTrace();
}
}
}
java设置代理
package com.path.test;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.net.URL;
import java.net.URLConnection;
public class TURLConnection {
public static void main(String args[]) throws Exception
{
//设置代理上外网
System.getProperties().put("proxySet", "true");
System.getProperties().put("proxyHost", "172.31.170.14");
System.getProperties().put("proxyPort", "8080");
/*
如果需要验证用户
//Authenticator.setDefault(new MyAuthenticator());
*/
URL url=new URL("http://www.csdn.net");
URLConnection urlCon=url.openConnection();
/*
* 方法1,一次过读取所有信息
*/
BufferedInputStream bis=new BufferedInputStream(urlCon.getInputStream());
BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("C:aa.htm"));
byte b[]=new byte[bis.available()];
bis.read(b);
bos.write(b);
bos.flush();
bis.close();
bos.close();
/*
* 方法2,一个个字节地读取
*/
// InputStream is=urlCon.getInputStream();
// FileOutputStream fos=new FileOutputStream("C:bb.htm");
// int tmp=0;
// while((tmp=is.read())!=-1)
// {
// fos.write(tmp);
// }
// fos.flush();
// fos.close();
// is.close();
}
}
package com.path.test;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
public class MyAuthenticator extends Authenticator {
private String name ;
private String password;
public MyAuthenticator() {
super();
// TODO Auto-generated constructor stub
}
public MyAuthenticator(String name, String password) {
super();
// TODO Auto-generated constructor stub
this.name = name;
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(this.getName(),this.getPassword().toCharArray());
}
}