HTTP requests have eight of methods Get,post,head,put,delete,options,trace,connect,get and post are more commonly used, as a result of project development needs, began to study the put request method, in fact, and the Post request method is similar. Here are the common request methods and the less commonly used put methods.
The head method is the same as the Get method, except that the message body is not returned when the server responds. In a response to a head request, the original information contained in the HTTP header should be the same as the response message of a GET request. This method can be used to get the meta information implied in the request without transferring the entity itself. It is also often used to test the validity, availability, and recent modifications of hyperlinks.
Head requests are often ignored, but can provide a lot of useful information, especially with limited speed and bandwidth. Mainly has the following characteristics:
1, only the first request resources;
2, check the effectiveness of hyperlinks;
3, check whether the Web page has been modified;
4, more for automatic search robot to get the logo information of the Web page, to obtain RSS seed information, or to pass security certification information, etc.
The GET request method is one of the most common HTTP requests, with the following features:
1, the default request method;
2, GET request is usually used to obtain information, so it should be safe, idempotent.
3. The request data is displayed on the URL and sent in the form of name/value. There is a limit to the length of the request,
4, in IE and opera and other browsers will generate URL cache. If no redundant request parameters are added, the response returns data in the cache, resulting in inconsistent results.
5, Low security. is directly behind the request header and is plaintext.
The Post method marks the existing resource and submits the data to expand the database by appending operations
Post methods have the following characteristics:
1, mainly used to submit data to the server, and get is mainly used for access;
2, the data is encapsulated in the request, but not the URL, therefore does not have the length limit;
3, can not cache, and get request cache, in IE and other browsers will directly return cached data.
Four, Put method
The Put method is typically used to send a request to the server, and if the URI does not exist, the server is required to create resources based on the request, and if so, the server accepts the request content and modifies the original version of the URI resource. is commonly known as the upload resources.
http/1.1 does not define how a put request affects the state of the original server, and the put request must comply with the information transfer requirements.
Directly on the code:
public static void Getuploadinformation (String path,string obj) throws IOException, Jsonexception {//Create connection
URL url = new URL (path);
HttpURLConnection connection;
StringBuffer Sbuffer=null;
try {//Add request Content connection= (httpurlconnection) url.openconnection ();
Sets the HTTP Connection property connection.setdooutput (TRUE);//HTTP body, so it needs to be set to true, by default, false;
Connection.setdoinput (TRUE);//setting is read from HttpURLConnection, true by default; Connection.setrequestmethod ("put");
You can submit a GET, POST, DELETE, and put HTTP-provided functionality//connection.setusecaches (false) as needed, and/or set the cache, note that setting the request method to POST cannot be cached
Connection.setinstancefollowredirects (TRUE); Connection.setrequestproperty ("Host", "*******"); Set the requested server URL, domain name, such as ***.**.***.*** connection.setrequestproperty ("Content-type", "Application/json");/Set Request format JSON, you can also set XML-formatted Connection.setrequestproperty ("Accept-charset", "utf-8"); //Set the encoding language Connection.setrequestproperty ("X-auth-token", "Token"); Set the requested token Connection.setrequestproperty ("Connection", "keep-alive"); Setting the status of a connection
Connection.setrequestproperty ("transfer-encoding", "chunked");/Set transport encoding
Connection.setrequestproperty ("Content-length", obj.tostring (). GetBytes (). Length + ""); To set the length of a file request
Connection.setreadtimeout (10000);/Set Read timeout
Connection.setconnecttimeout (10000);/Set the connection timeout time
Connection.connect ();
OutputStream out = Connection.getoutputstream ()//writes out the data to the object output stream, which is stored in the memory buffer
Out.write (Obj.tostring (). GetBytes ()); Out.write (New String ("Test Data"). GetBytes ()); Refreshes the object output stream, writes any bytes into the potential stream
Out.flush ();
Closes the stream object, at which point no data can be written to the object output stream and the previously written data exists in the memory buffer
Out.close ();
Read Response
if (Connection.getresponsecode () ==200) {
Get an input stream from the server
InputStreamReader InputStream = new InputStreamReader (Connection.getinputstream ()); Invokes the getInputStream () function of the HttpURLConnection connection object to send the full HTTP request message encapsulated in the memory buffer to the server.
BufferedReader reader = new BufferedReader (InputStream);
String lines;
sbuffer= New StringBuffer ("");
while ((lines = Reader.readline ())!= null) {
lines = new String (Lines.getbytes (), "Utf-8");
Sbuffer.append (lines); }
Reader.close ();
}else{
LOG.I (TAG, "Request Failed" +connection.getresponsecode ());
}
Disconnect
Connection.disconnect ();
catch (IOException e) {
E.printstacktrace ();
}
}
JSON data
Public static String Queryloginbody (String type,string userid,string checksum) {
string json= "{\ type\": \ "" + Type+ "\", "+" \ "jid\": "" "+userid+" \ "" + "\" checksum\ ": \" +checksum+ "\"} ";
return JSON;
}
Call the method, enter the arguments you want to pass in, and then just put the JSON data in.
String json=apputils.queryloginbody ("login", "usr", "123132");
Apputils.getuploadinformation ("http://www.xxx.com", JSON);
Reference Links:
http://blog.csdn.net/CrystalDestiny/article/details/46469465
StackOverflow Forum:
Http://stackoverflow.com/questions/15678208/making-put-request-with-json-data-using-httpurlconnection-is-not-working
Android Practical Source Link: http://www.apkbus.com/?fromuser=StruggleLin