Use HttpURLConnection to send post and get requests to the server (GO)

Source: Internet
Author: User
Tags response code

One, using HttpURLConnection to send a GET request to the server

1. Send a GET request to the server

@Test
PublicvoidSendsms ()Throwsexception{
String message="The goods have been sent to";
Message=Urlencoder.encode (Message,"UTF-8");
SYSTEM.OUT.PRINTLN (message);
String Path="Http://localhost:8083/DS_Trade/mobile/sim!add.do?message="+Message
URL URL=NewURL (path);
HttpURLConnection Conn=(httpurlconnection) url.openconnection ();
Conn.setconnecttimeout (5*1000 Conn.setrequestmethod ( "get ");
InputStream instream = Conn.getinputstream ();
byte["Data = Streamtool.readinputstream (instream);
String result=new String (data, "utf-8" Span style= "color: #000000;" >);
System.out.println (Result);
} /span>

2. Read data from the server

String message=Request.getparameter ("message");     

Second, use HttpURLConnection to send a POST request to the server

1. Send a POST request to the server

@Test
PublicvoidAddbyurl ()Throwsexception{
String encoding="UTF-8";
String params="[{\ "addtime\": \ "2011-09-19 14:23:02\" [],\ "iccid\": \ "1111\", \ "id\": 0,\ "imei\": \ "2222\", \ "imsi\": \ "3333\", \ " Phonetype\ ": \" 4444\ ", \" Remark\ ": \" aaaa\ ", \" tel\ ": \" 5555\ "}]";
String Path="Http://localhost:8083/xxxx/xxx/sim!add.do";
Byte[] Data=Params.getbytes (encoding);
URL URL=NewURL (path);
HttpURLConnection Conn=(httpurlconnection) url.openconnection ();
Conn.setrequestmethod ("POST");
Conn.setdooutput (True);
//Application/x-javascript text/xml->xml Data Application/x-javascript->json Object application/ x-www-form-urlencoded-> form data
Conn.setrequestproperty ("Content-type","Application/x-javascript; charset="+encoding);
Conn.setrequestproperty ("Content-length" Conn.setconnecttimeout (5* 1000);
OutputStream outstream = Conn.getoutputstream ();
Outstream.write (data);
Outstream.flush ();
Outstream.close ();
System.out.println (Conn.getresponsecode ()); // response code 200 indicates success
if (Conn.getresponsecode () ==200) {
inputstream instream = Conn.getinputstream ();  
string result=new String ( Streamtool.readinputstream (instream), "UTF-8");
}
/span>

2. Read data from the server

 // Get the data from the POST request  
byte=streamtool.readinputstream (Request.getinputstream ());
//[{\ "addtime\": \ "2011-09-19 14:23:02\" [],\] Iccid\ ": \" 1111\ ", \" id\ ": 0,\" imei\ ": \" 2222\ ", \" imsi\ ": \" 3333\ ", \" phonetype\ ": \" 4444\ ", \" Remark\ ": \" Aaaa\ "," tel \ ": \" 5555\ "}]
String JSON =new String (data, Span style= "color: #000000;" > "utf-8 );

Http://www.cnblogs.com/linjiqin/archive/2011/09/19/2181634.html

Use HttpURLConnection to send post and get requests to the server (GO)

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.