Java Get and POST requests

Source: Internet
Author: User

From the surface, get and POST requests:

A GET request is to enclose the request body directly after the URL, using "?" between the URL and the request body. Split, between the different parameters with "&" separated,%xx xx for the symbol in 16 binary ASCII, if the data is English letter/number, sent as is, if it is a space, converted to +, if it is Chinese/other characters, the string is directly encrypted with BASE64.

Post submits the data is placed in the packet of the HTTP packet, post does not limit the data submitted. Post is safer than get, when the data is in Chinese or not sensitive data, then use GET, because use GET, parameters will be displayed in the address, for sensitive data and not Chinese characters of data, then use post

Get requests in Java:

Package Com.httpgettest;import Java.io.bufferedreader;import Java.io.ioexception;import java.io.InputStream;import Java.io.inputstreamreader;import Java.net.malformedurlexception;import Java.net.url;import java.net.URLConnection    ;p Ublic class Httpgettest {public static void main (string[] args) {Creathttpurl creaturl = new Creathttpurl ();    String url = creathttpurl.creaturl ("Teacher", "en", "zh-chs");       System.out. Print (URL);    New Readbyget (). Start (); ReadData ();}    public static void ReadData () {try {creathttpurl creaturl = new Creathttpurl (); String urlstr = Creathttpurl.creaturl ("Teacher", "en", "zh-chs"); URL url = new URL (urlstr); URLConnection connection = Url.openconnection (); InputStream is = Connection.getinputstream (); InputStreamReader ISR = New InputStreamReader (IS); BufferedReader bf = new BufferedReader (ISR); String Line; StringBuilder builder = new StringBuilder (); while (line = Bf.readline ())! = null) {builder.append (line);} Bf.close (); Isr.close (); Is.close (); SyStem.out.println (builder); } catch (Exception e) {e.printstacktrace ();}}}

Post Requests in Java:

Package Com.httpposttest;import Java.io.bufferedreader;import Java.io.bufferedwriter;import java.io.IOException; Import Java.io.inputstream;import Java.io.inputstreamreader;import Java.io.outputstream;import Java.io.outputstreamwriter;import Java.net.httpurlconnection;import Java.net.malformedurlexception;import Java.net.url;import Java.net.urlconnection;public class Httpposttest {public static void main (string[] args) { Creathttpurl Creaturl = new Creathttpurl (); String urlstr = "Https://openapi.youdao.com/api"; String paramstr = Creaturl.createparam ("Good", "en", "zh-chs"); System.out.println (PARAMSTR); try {//Set request related Settings URL url = new URL (urlstr); HttpURLConnection connection = (httpurlconnection) url.openconnection (); Connection.setrequestproperty ("Encoding", " UTF-8 "); Connection.setrequestmethod (" POST "); Connection.setdoinput (true); Connection.setdooutput (true);// Post submission data outputstream OS = Connection.getoutputstream (); OutputStreamWriter OSW = new OutputStreamWriter (OS); BufferedWriter writer = new BUfferedwriter (OSW); Writer.write (PARAMSTR); Writer.flush ();//Read data obtained inputstream is = Connection.getinputstream (); I Nputstreamreader ISR = new InputStreamReader (IS); BufferedReader br = new BufferedReader (ISR); String Line; StringBuilder builder = new StringBuilder (); while (line = Br.readline ())! = null) {builder.append (line);} Writer.close (); Osw.close (); Os.close (); Br.close (); Isr.close (); Is.close (); System.out.print (Builder.tostring ());} catch (Exception e) {e.printstacktrace ();}}}

Java Get and POST requests

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.