复制代码代码如下 :
패키지 테스트;
import java.io.bufferedReader;
import java.io.ioexception;
import java.io.inputstream;
import java.io.inputStreamReader;
java.net.authenticator import;
import java.net.httpurlConnection;
java.net.passwordauthentication import;
import java.net.url;
import java.net.urlConnection;
java.util.properties import;
공개 클래스 urltest {
// 一个 public 一个 一个, 返回字符串, 错误则返回 "오류 오픈 URL"
public static string getContent (String Strurl) {
노력하다 {
url url = new URL (strurl);
bufferedReader br = new bufferedReader (new inputStreamReader (url
.OpenStream ());
문자열 s = "";
StringBuffer sb = new StringBuffer ( "");
while ((s = br.readline ())! = null) {
sb.append (s + "/r/n");
}
br.close ();
반환 sb.toString ();
} catch (예외 e) {
"오류 오픈 URL :" + strurl;
}
}
public static void initproxy (문자열 호스트, int 포트, 최종 문자열 사용자 이름,
최종 문자열 암호) {
Authenticator.setDefault (new Authenticator () {
보호 된 PasswordAuthentication getPasswordAuthentication () {
새 비밀번호를 반환합니다 (사용자 이름,
새 문자열 (비밀번호) .tochararray ());
}
});
System.SetProperty ( "http.proxyType", "4");
System.SetProperty ( "http.proxyport", integer.tostring (port));
System.SetProperty ( "http.proxyHost", 호스트);
System.SetProperty ( "http.proxyset", "true");
}
public static void main (String [] args)은 ioexception {
문자열 url = "//www.vevb.com";
문자열 proxy = "http://192.168.22.81";
int port = 80;
문자열 username = "username";
문자열 비밀번호 = "비밀번호";
문자열 curline = "";
문자열 내용 = "";
URL Server = 새 URL (URL);
InitProxy (프록시, 포트, 사용자 이름, 비밀번호);
httpurlConnection Connection = (httpurlConnection) 서버
.openConnection ();
Connection.connect ();
inputStream은 = connection.getInputStream ();
BufferedReader reader = New BufferedReader (새
inputStreamReader (is));
while ((curline = reader.readline ())! = null) {
content = content + curline + "/r/n";
}
System.out.println ( "content =" + content);
is.close ();
System.out.println (getContent (url));
}
}