为什么它给我一个405错误,尽管代码应该在逻辑上工作,链接有一个有效的下载,我从互联网上复制一些工作代码,看看发生了什么,如这个主题如何下载一个图像与Java socket HTTP / 1.1请求?它也没有给我一个响应200?
很抱歉混乱的代码我试图找出问题大约2天
//package htmlconnection;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.Socket;
import java.net.URL;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import javax.net.SocketFactory;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSocketFactory;
public class MyClass {
public static void main(String args[])throws UnknownHostException, IOException {
try {
URL url =new URL("http://www.ncert.nic.in/NCERTS/l/jemh1an.pdf");
String path=url.getPath();
String domain=url.getHost();
System.out.println(path);
System.out.println(domain);
Socket socket = new Socket(domain,80);
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())));
System.out.println(socket.isConnected());
out.println("Get "+path+" HTTP/1.1\n" +"Host: "+domain);
out.println();
out.flush();
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null && inputLine.trim() != "0") {
System.out.println(inputLine);
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
莫回无
四季花海
相关分类