我想使用套接字访问任意网页(作为我自己的学习机制)。下面的代码不起作用,我做错了什么?
import java.net.*;
import java.io.*;
public class Example
{
public static void main(String args[]) throws Exception
{
Socket socket =
new Socket("www.google.com", 80);
PrintWriter out =
new PrintWriter(socket.getOutputStream(), true);
BufferedReader reader =
new BufferedReader(
new InputStreamReader(socket.getInputStream()));
BufferedReader stdIn =
new BufferedReader(
new InputStreamReader(System.in));
String userInput;
while ((userInput = stdIn.readLine()) != null) {
out.println("GET / HTTP/1.1 \\r\\n Host: www.google.com \\r\\n\\r\\n");
System.out.println("echo: " + reader.readLine());
}
}
}
在尝试了几个小时之后,我无法弄清楚究竟是我做错了什么。我想要的只是谷歌或其他一些网站的主页。任何人都可以帮助我吗?
答案 0 :(得分:1)
您可以尝试以下内容(使用套接字)
package com.example.webpagesocket;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
public class GetWebPageUsingSockets {
public static void main(String[] args) {
String urlString;
urlString = "www.google.com";
accessWeb(urlString);
}
private static void accessWeb(String urlString) {
String host;
String page;
int slashLoc;
// Set up encoding and decoding
Charset charset = Charset.forName("ISO-8859-1");
CharsetDecoder decoder = charset.newDecoder();
CharsetEncoder encoder = charset.newEncoder();
if ((slashLoc = urlString.indexOf('/')) < 0) {
host = urlString;
page = "";
} else {
host = urlString.substring(0, slashLoc);
page = urlString.substring(slashLoc);
}
System.out.println("Accessing web page demonstration");
System.out.println("Host: '" + host + "' Page: '" + page + "'");
SocketChannel channel = null;
try {
ByteBuffer buffer = ByteBuffer.allocateDirect(1024);
CharBuffer charBuffer = CharBuffer.allocate(1024);
InetSocketAddress socketAddress = new InetSocketAddress(host, 80);
channel = SocketChannel.open();
channel.connect(socketAddress);
String request = "GET " + page + " \r\n\r\n";
channel.write(encoder.encode(CharBuffer.wrap(request)));
while ((channel.read(buffer)) != -1) {
buffer.flip();
decoder.decode(buffer, charBuffer, false);
charBuffer.flip();
System.out.println(charBuffer);
buffer.clear();
charBuffer.clear();
}
} catch (UnknownHostException e) {
System.err.println(e);
} catch (IOException e) {
System.err.println(e);
} finally {
if (channel != null) {
try {
channel.close();
} catch (IOException ignored) {
}
}
}
System.out.println("\nDone.");
}
}
答案 1 :(得分:0)
编辑:在第一次阅读时没有注意到......
看起来您不断向主机发送"GET / HTTP/1.1 \\r\\n Host: www.google.com \\r\\n\\r\\n"
(每次阅读时)。您最好将其保持在循环之外并发送真carriage return + line feed
而不是可打印的字符\ r \ n
:
out.println("GET / HTTP/1.1 \r\nHost: www.google.com\r\n\r\n");
String userInput;
while ((userInput = stdIn.readLine()) != null) {
您只能在发送GET
后开始阅读,但套接字将缓冲数据。