Java by API/javax.net/Socket
Версия от 17:43, 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));
}
}