Hello I'm trying to make a basic api that send http requests to any server using socket
When i'm trying to send a http request to google.com it sends it very well but when i send a GET http request to any server it sends this
HTTP/1.1 301 Moved Permanently Content-Length: 0 Location: https://github.com/ connection: close
This is the source code
import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.InetAddress; import java.net.Socket; import java.net.URI; import java.net.UnknownHostException; public class socketsAPI { private String method = null; private final String url; private boolean output = false; private String payload; private String response; private int statuscode; private final StringBuilder headers = new StringBuilder(); public socketsAPI(String url){ this.url = url; } public String getResponse(){ return this.response; } public Integer getResponseCode(){ return this.statuscode; } public void setMethod(String method){ this.method = method; } public void setGETOutput(boolean output){ this.output = output; } public void setPayload(String payload){ this.payload = payload; } public void setRequestProperty(String first,String second){ headers.append(first).append(":").append(second).append("\r\n"); } public void connect() { try { // Parsing section~ // Getting the domain with path URI uri = new URI(url); String host = uri.getHost(); String path = uri.getPath(); final int port = 80; // Sending request section~ // Getting the ip InetAddress inetAddress = InetAddress.getByName(host); // Opening the socket Socket socket = new Socket(inetAddress, port); // Getting input and output stream of the socket InputStream input = socket.getInputStream(); OutputStream out = socket.getOutputStream(); if(method.equalsIgnoreCase("GET")){ if(output){ String request = "GET " + path + "?" + this.payload + " HTTP/1.0\r\n" + this.headers.toString() + "Connection: Close\r\n\r\n"; out.write(request.getBytes()); out.flush(); }else{ String request = "GET " + path + " HTTP/1.0\r\n" + this.headers.toString() + "Connection: Close\r\n\r\n"; out.write(request.getBytes()); out.flush(); } }else if(method.equalsIgnoreCase("POST")){ String request = "POST " + path + " HTTP/1.0\r\n" + this.headers.toString() + "\r\n" + this.payload; out.write(request.getBytes()); out.flush(); }else if(method.equalsIgnoreCase("PUT")){ String request = "PUT " + path + " HTTP/1.0\r\n" + this.headers.toString() + "\r\n" + this.payload; out.write(request.getBytes()); out.flush(); } else{ System.out.println("Invalid HTTP method"); socket.close(); } BufferedReader reader = new BufferedReader(new InputStreamReader(input)); StringBuilder builder = new StringBuilder(); String lines; while ((lines = reader.readLine()) != null){ builder.append(lines).append(" ").append("\n"); } String socketresponse = builder.toString(); String[] sag = socketresponse.split(" "); this.statuscode = Integer.parseInt(sag[1]); this.response = socketresponse; socket.close(); }catch (Exception ex){ ex.printStackTrace(); } } }
socketsAPI sockets = new socketsAPI("https://github.com/"); sockets.setMethod("GET"); sockets.setRequestProperty("Host","github.com"); sockets.connect(); System.out.println("Ended with " + sockets.getResponseCode() + " response: " + sockets.getResponse());
And thanks for your help!
https://stackoverflow.com/questions/66961799/http-1-1-301-moved-permanently April 06, 2021 at 09:56AM
没有评论:
发表评论