java - Create and log in to a web site with http POST request without using HttpUrlConnection library or other libraries -


i trying create http post request without using library such java.net.httpurlconnection, java.net.urlconnection, url.openconnection() etc in java. every example on internet has used libraries such httpurlconnection. can please me create post request login in web site in java?

you need use low level networking working directly java.net.socket objects. perhaps best place start java sockets tutorial, , check out sample echo client therein. show how connect server , read , write data to/from it.

in order construct required messages need understand http post. topic far detailed discuss here, need reading.

here program sends simple hard-coded post request server running on localhost:1234:

import java.io.*; import java.net.*;  public class sendpost {     public static void main(string[] args) {         try {             string post_request = "post /login http/1.1\r\nhost: localhost:1234\r\ncontent-length: 41\r\ncontent-type: application/x-www-form-urlencoded\r\n\r\nusername=bob%40test.com&password=i+forget";              socket s = new socket("localhost", 1234);             dataoutputstream out = new dataoutputstream(s.getoutputstream());                        out.writebytes(post_request);                       s.close();         } catch(exception e) {             system.out.println(e);         }     } } 

this sends post request containing user name , password using same format browser if using submit login form.

the (heavily simplified) request looks this:

 post /login http/1.1 host: localhost:1234 content-length: 41 content-type: application/x-www-form-urlencoded  username=bob%40test.com&password=i+forget 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -