Java by API/javax.net/Socket — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 14:35, 31 мая 2010
new Socket(String addr,int port)
/*
* Output:
*
*
HTTP/1.1 400 Bad Request
Content-Type: text/html
Date: Mon, 24 Apr 2006 23:11:15 GMT
Connection: close
Content-Length: 39
<h1>Bad Request (Invalid Hostname)</h1>
*/
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
public class MainClass {
public static void main(String args[]) throws Exception {
byte buff[] = new byte[1024];
InetAddress addr = InetAddress.getByName("www.jexp.ru");
Socket s = new Socket(addr,80);
OutputStream output = s.getOutputStream();
InputStream input = s.getInputStream();
String GetCmd = "GET /index.html HTTP/1.0\r\n\r\n";
GetCmd.getBytes(0,GetCmd.length(),buff,0);
output.write(buff);
input.read(buff,0,buff.length);
System.out.println(new String(buff,0));
}
}
Socket: getInputStream()
/*
* Output:
*
*
HTTP/1.1 400 Bad Request
Content-Type: text/html
Date: Mon, 24 Apr 2006 23:11:15 GMT
Connection: close
Content-Length: 39
<h1>Bad Request (Invalid Hostname)</h1>
*/
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
public class MainClass {
public static void main(String args[]) throws Exception {
byte buff[] = new byte[1024];
InetAddress addr = InetAddress.getByName("www.jexp.ru");
Socket s = new Socket(addr,80);
OutputStream output = s.getOutputStream();
InputStream input = s.getInputStream();
String GetCmd = "GET /index.html HTTP/1.0\r\n\r\n";
GetCmd.getBytes(0,GetCmd.length(),buff,0);
output.write(buff);
input.read(buff,0,buff.length);
System.out.println(new String(buff,0));
}
}
Socket: getOutputStream()
/*
* Output:
*
*
HTTP/1.1 400 Bad Request
Content-Type: text/html
Date: Mon, 24 Apr 2006 23:11:15 GMT
Connection: close
Content-Length: 39
<h1>Bad Request (Invalid Hostname)</h1>
*/
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
public class MainClass {
public static void main(String args[]) throws Exception {
byte buff[] = new byte[1024];
InetAddress addr = InetAddress.getByName("www.jexp.ru");
Socket s = new Socket(addr,80);
OutputStream output = s.getOutputStream();
InputStream input = s.getInputStream();
String GetCmd = "GET /index.html HTTP/1.0\r\n\r\n";
GetCmd.getBytes(0,GetCmd.length(),buff,0);
output.write(buff);
input.read(buff,0,buff.length);
System.out.println(new String(buff,0));
}
}