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)