Java_traine
چهارشنبه 20 آبان 1388, 16:15 عصر
من مي خواهم با استفاده از Socket به اينترنت وصل شوم اين کار با استفاده از کد زير به آساني امکان پذير است:
int ch;
Socket socket = new Socket("www.google.com", 80);
InputStream in = socket.getInputStream();
OutputStream out = socket.getOutputStream();
String str = "GET /index.html HTTP/1.0\n\n";
byte buf[] = str.getBytes();
out.write(buf);
while ((ch = in.read()) != -1)
System.out.print((char) ch);
socket.close();
اين کد ، صفحه اصلي گوگل را خوانده و در کنسول چاپ مي کند.
حال اگر اتصال کامپيوتر بصورت مستقيم نباشد و کامپيوتر در محل شرکت يا اداره و از طريق يک پراکسي به اينترنت وصل شده باشد چگونه بايد عمل کرد. به عبارت ديگر اگر مشخصات پراکسي شرکت مانند زير باشد تغييرات کد بالا چه خواهد بود:
Proxy: 10.21.0.4
Port:3128
user: ali
password: 1234
در ضمن کد هاي زير را امتحان کرده ام و جواب نگرفته ام
Authenticator.setDefault(new Authenticator()
{
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication("ali", "1234".toCharArray());
}
});
System.setProperty("http.proxyHost","10.21.0.4");
System.setProperty("http.proxyPort","3128");
// System.getProperties().setProperty("http.proxyHost","10.21.0.4");
// System.getProperties().setProperty("http.proxyPort","3128");
SocketAddress proxyAddress = new InetSocketAddress("10.21.0.4", 3128);
Proxy proxy = new Proxy(Proxy.Type.HTTP, proxyAddress);
Socket socket = new Socket(proxy);
SocketAddress internetSocketAddress = new InetSocketAddress("www.google.com", 80);
socket.connect(internetSocketAddress);
// Socket socket = new Socket("www.yahoo.com",80);
InputStream in = socket.getInputStream();
OutputStream out = socket.getOutputStream();
out.write("GET /index.html HTTP/1.0\n\n".getBytes());
int ch;
while ((ch = in.read()) != -1)
System.out.println(ch);
socket.close();
از اينکه براي راهنمايي من وقت خواهيد گذاشت ممنونم.
int ch;
Socket socket = new Socket("www.google.com", 80);
InputStream in = socket.getInputStream();
OutputStream out = socket.getOutputStream();
String str = "GET /index.html HTTP/1.0\n\n";
byte buf[] = str.getBytes();
out.write(buf);
while ((ch = in.read()) != -1)
System.out.print((char) ch);
socket.close();
اين کد ، صفحه اصلي گوگل را خوانده و در کنسول چاپ مي کند.
حال اگر اتصال کامپيوتر بصورت مستقيم نباشد و کامپيوتر در محل شرکت يا اداره و از طريق يک پراکسي به اينترنت وصل شده باشد چگونه بايد عمل کرد. به عبارت ديگر اگر مشخصات پراکسي شرکت مانند زير باشد تغييرات کد بالا چه خواهد بود:
Proxy: 10.21.0.4
Port:3128
user: ali
password: 1234
در ضمن کد هاي زير را امتحان کرده ام و جواب نگرفته ام
Authenticator.setDefault(new Authenticator()
{
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication("ali", "1234".toCharArray());
}
});
System.setProperty("http.proxyHost","10.21.0.4");
System.setProperty("http.proxyPort","3128");
// System.getProperties().setProperty("http.proxyHost","10.21.0.4");
// System.getProperties().setProperty("http.proxyPort","3128");
SocketAddress proxyAddress = new InetSocketAddress("10.21.0.4", 3128);
Proxy proxy = new Proxy(Proxy.Type.HTTP, proxyAddress);
Socket socket = new Socket(proxy);
SocketAddress internetSocketAddress = new InetSocketAddress("www.google.com", 80);
socket.connect(internetSocketAddress);
// Socket socket = new Socket("www.yahoo.com",80);
InputStream in = socket.getInputStream();
OutputStream out = socket.getOutputStream();
out.write("GET /index.html HTTP/1.0\n\n".getBytes());
int ch;
while ((ch = in.read()) != -1)
System.out.println(ch);
socket.close();
از اينکه براي راهنمايي من وقت خواهيد گذاشت ممنونم.