java - Http request using sockets -


I have some requests using HTTP requests that are embedded in the HTTP server. I've been using sockets so far, I did this, but I'm losing a bit:

  URI Yuri = URI Make (raw data); Try {String host = uri.getHost (); String path = uri.getRawPath (); If (path == null || path.length () == 0) {path = "/"; } String protocol = uri.getScheme (); Int port = uri.getPort (); If (port == -1) {if (protocol.equals ("http")) {port = 80; // http port} and if (protocol.equals ("https")) {port = 443; // https port}} socket socket = new socket (host, port); PrintWriter Request = New Print Witter (socket.getOutputStream ()); Request.print ("GET" + Path + "HTTP / 1.1 \ r \ n" + "Host:" + Host + "\ r \ n" + "Connection: Closed \ r \ n \ r \ n"); Request.flush ();   

Do I need to do this? An example of RawData is "http: // somemessage" . Is the protocol written correctly? Thanks

Do I need to do this?

No.

  1. You need a reaction process, and there is complexity in dealing with different types of reactions. / P>

  2. The way you are handling HTTPS is all wrong you need to deal with SSL connection talks ... and if you try to do so You have to do a lot of coding using plain sockets.

    An example of a raw data "http: // somemessage"

    a strange What is the URL. After "//" the stuff (or should be included) must be a resolvable hostname or IP address if you try to fetch a URL using a web browser, then it will not work.

    Is the protocol written correctly?

    The URL is not written correctly if you try to use that so-called URL with HttpURLConnection , or other client-side HTTP API (see below), it will not work !!!

    A typical well-formed URL (in this case, with a clear port number) looks like this:

      http://example.com: 8080 / path / to / resource   

    You can also express the URI as a relative; For example

      http: / path / to / source   

    or

      http: relpath / to / resource   

    or even

      resource   

    but before both of these forms are completed (URL ) The form can be used by client libraries.


    But obviously, this is the wrong way to go about things. The existing implementation of the client-side HTTP / HTTPS stack in Java SE libraries, and Apache is also in HTTP libraries. The attempt to re-complement them is a waste of time.

Comments

Popular posts from this blog

c - Mpirun hangs when mpi send and recieve is put in a loop -

python - Apply coupon to a customer's subscription based on non-stripe related actions on the site -

java - Unable to get JDBC connection in Spring application to MySQL -