JAVA HTTP sends JSON data

Source: Internet
Author: User

Http://www.cnblogs.com/snake-hand/p/3157332.html

http://rain-2372.iteye.com/blog/1860431

Package com.test;

Import Java.io.BufferedReader;
Import Java.io.DataOutputStream;
Import java.io.IOException;
Import Java.io.InputStreamReader;
Import java.io.UnsupportedEncodingException;
Import java.net.HttpURLConnection;
Import java.net.MalformedURLException;
Import Java.net.URL;
Import Net.sf.json.JSONObject;

public class Appaddtest {

public static final String Add_url = "Http://localhost:16000/app/add";

public static void Appadd () {

try {
Create a connection
URL url = new URL (add_url);
HttpURLConnection connection = (httpurlconnection) URL
. OpenConnection ();
Connection.setdooutput (TRUE);
Connection.setdoinput (TRUE);
Connection.setrequestmethod ("POST");
Connection.setusecaches (FALSE);
Connection.setinstancefollowredirects (TRUE);
Connection.setrequestproperty ("Content-type",
"application/x-www-form-urlencoded");

Connection.connect ();

POST request
DataOutputStream out = new DataOutputStream (
Connection.getoutputstream ());
Jsonobject obj = new Jsonobject ();
Obj.element ("App_name", "asdf");
Obj.element ("App_ip", "10.21.243.234");
Obj.element ("App_port", 8080);
Obj.element ("App_type", "001");
Obj.element ("App_area", "ASD");

Out.writebytes (Obj.tostring ());
Out.flush ();
Out.close ();

Read response
BufferedReader reader = new BufferedReader (New InputStreamReader (
Connection.getinputstream ()));
String lines;
StringBuffer sb = new StringBuffer ("");
while ((lines = Reader.readline ()) = null) {
lines = new String (Lines.getbytes (), "utf-8");
Sb.append (lines);
}
System.out.println (SB);
Reader.close ();
Disconnect Connection
Connection.disconnect ();
} catch (Malformedurlexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (Unsupportedencodingexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}

}

public static void Main (string[] args) {
Appadd ();
}

}
Turn http://www.blogjava.net/jlins-you/archive/2012/06/22/381290.html

JAVA HTTP sends JSON data

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.