Post commits, first sending a request to the server and then the server responding.
The request sent here is the user name and password, and if the response is correct, the error is also.
First we need to add the username and password to the URL. Here we have to deal with this first, then get the link, and send the requested action. The response is then received.
Package com.neusoft.httpdemo; ImportJava.io.ByteArrayOutputStream;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.io.OutputStream;Importjava.io.UnsupportedEncodingException;Importjava.net.HttpURLConnection;Importjava.net.MalformedURLException;ImportJava.net.URL;ImportJava.net.URLEncoder;ImportJava.util.HashMap;ImportJava.util.Map; Public classPost {Private StaticString PATH = "Http://192.168.1.119:8080/ok/servlet/Login"; Private StaticURL url =NULL; Static { Try{URL=NewURL (PATH); } Catch(malformedurlexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } } //fill in the URL parameter, which is the user name and password. The second one is the character set Private StaticString sendpostmessage (map<string, string>params, String encode) { //used to put the parameters to be passed, buffer is indefinite.StringBuffer buffer =NewStringBuffer (); Try { if(Params! =NULL&&!Params.isempty ()) { //traverse the contents of a map for(Map.entry<string, string>Entry:params.entrySet ()) {Buffer.append (Entry.getkey ()). Append ("="). Append (Urlencoder.encode (Entry.getvalue (), encode)). Append ( "&"); } //Remove the last & numberBuffer.deletecharat (Buffer.length ()-1); //Get linksHttpURLConnection httpurlconnection =(httpurlconnection) URL. OpenConnection (); Httpurlconnection.setconnecttimeout (3000); Httpurlconnection.setdoinput (true); Httpurlconnection.setdooutput (true); //or the length and length of the information to be uploaded byte[] data =buffer.tostring (). GetBytes (); //if the browser will have built-in HTTP protocol, but we use Java or mobile access to no such protocol//We're going to set the request properties .//Set request body is text type//application/x-www-form-urlencoded such a party//will you turn your incoming data into a form of key=value? Add to URL afterHttpurlconnection.setrequestproperty ("Content-type", "Application/x-www-form-urlencoded"); Httpurlconnection.setrequestproperty ("Content-length", String.valueof (data.length)); //get the output streamOutputStream OutputStream =Httpurlconnection.getoutputstream (); Outputstream.write (data); intResponsecode =Httpurlconnection.getresponsecode (); System.out.println (Responsecode); if(Responsecode = = 200) { //because the method returns a string, now we get a//input stream, so we're going to convert it into a string by a method returnChangeinputstream (Httpurlconnection.getinputstream (), encode); } } } Catch(unsupportedencodingexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } return""; } //serializes an input to a specified encoded string Private Staticstring Changeinputstream (InputStream InputStream, string encode) {Bytearrayoutputstream ARRAYOUTPU TStream=NewBytearrayoutputstream (); intLen = 0; byte[] MyData =New byte[1024]; String result= ""; if(InputStream! =NULL) { Try { while(len = Inputstream.read (mydata))! =-1) {arrayoutputstream.write (MyData,0, Len); } result=NewString (Arrayoutputstream.tobytearray ()); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } } returnresult; } Public Static voidMain (string[] args) {Map<string, string> map =NewHashmap<string, string>(); Map.put ("username", "admin"); Map.put ("Password", "123"); String Resule= Sendpostmessage (Map, "Utf-8")); System.out.println (Resule); }}
HTTP protocol Post