⑴ 怎樣通過java使用socks代理訪問伺服器
packagetest;
importjava.io.IOException;
importjava.util.Date;
importorg.apache.commons.httpclient.HttpClient;
importorg.apache.commons.httpclient.HttpException;
importorg.apache.commons.httpclient.UsernamePasswordCredentials;
importorg.apache.commons.httpclient.auth.AuthScope;
importorg.apache.commons.httpclient.methods.PostMethod;
publicclasstest
{
publicstaticvoidmain(Stringargs[])
{
HttpClienthc=newHttpClient();
System.out.println("開始時間:"+System.currentTimeMillis());
for(inti=0;i<=100;i++)
{
try
{
//每10秒才會保存一次
Thread.sleep(12000);
}catch(InterruptedExceptione1)
{
//TODOAuto-generatedcatchblock
e1.printStackTrace();
}
Dated=newDate();
PostMethodpm=newPostMethod(
"http://www.tongaichina.com/post.asp?type=int&name=click&time="
+d.getTime());
try
{
//這里寫代理地址及埠
hc.getHostConfiguration().setProxy("代理地址",埠);
//這里是用戶名與密碼
=(
"用戶名","密碼");
hc.getState().setProxyCredentials(AuthScope.ANY,creds);
hc.executeMethod(pm);
System.out.println(pm.getResponseBodyAsString());
}catch(HttpExceptione)
{
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(IOExceptione)
{
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
System.out.println(i);
}
System.out.println("結束時間:"+System.currentTimeMillis());
}
}