Java code sends get and post requests

Source: Internet
Author: User

/**
* Send a GET request
*
* @ Param URL
* @ Return jsonobject
*/

Public static string getajaxdatafromurlforsms (string URL ){
Long begin = system. currenttimemillis ();
Log.info ("Request:" + URL );
String result = NULL;
Bufferedreader RD = NULL;

Try {
URL u = new URL (URL );
Inputstream is = U. openstream ();
RD = new bufferedreader (New inputstreamreader (is, "UTF-8 "));
Stringbuffer sb = new stringbuffer ();

String line;
While (line = RD. Readline ())! = NULL ){
SB. append (line );
}

Long end = system. currenttimemillis ();
Log.info ("response [" + (end-begin) + "]:" + sb. tostring ());
Result = sb. tostring ();

} Catch (malformedurlexception e ){
E. printstacktrace ();
} Catch (ioexception e ){
E. printstacktrace ();
} Finally {
If (RD! = NULL ){
Try {
Rd. Close ();
} Catch (ioexception e ){
E. printstacktrace ();
}
}

}

Return result;
}

/**

* @ Return xmldata
*/
Public static document getxmldatafromurl (string URL ){
Long begin = system. currenttimemillis ();
Log.info ("Request:" + URL );
Saxbuilder = new saxbuilder ();
Document Doc = NULL;
Try {
URL u = new URL (URL );
Inputstream in = U. openstream ();

Doc = saxbuilder. Build (in );

} Catch (exception e ){
E. printstacktrace ();
}
If (log. isinfoenabled ()){
Long end = system. currenttimemillis ();
Xmloutputter outp = new xmloutputter ();
Log.info ("response [" + (end-begin) + "]:" + outp. outputstring (DOC ));
}

Return Doc;
}

/**

* Send a GET request
*
* @ Param URL
* URL
* @ Return jsonobject
*/

Public static jsonobject getjsondatafromurl (string URL ){
Long begin = system. currenttimemillis ();
Log.info ("Request:" + URL );
Jsonobject jsonobj = NULL;
Bufferedreader RD = NULL;

Try {
URL u = new URL (URL );
Inputstream is = U. openstream ();
RD = new bufferedreader (New inputstreamreader (is, "UTF-8 "));
Stringbuffer sb = new stringbuffer ();

String line;
While (line = RD. Readline ())! = NULL ){
SB. append (line );
}

Long end = system. currenttimemillis ();
Log.info ("response [" + (end-begin) + "]:" + sb. tostring ());
Jsonobj = new jsonobject (sb. tostring ());

} Catch (malformedurlexception e ){
E. printstacktrace ();
} Catch (ioexception e ){
E. printstacktrace ();
} Catch (jsonexception e ){
E. printstacktrace ();
} Finally {
If (RD! = NULL ){
Try {
Rd. Close ();
} Catch (ioexception e ){
E. printstacktrace ();
}
}

}

Return jsonobj;
}

/**
* Send a POST request
*
* @ Param URL
* URL address Params Parameters
* @ Return jsonobject
*/

Public static jsonobject getjsondatafromurl (string URL, string Params ){
Long begin = system. currenttimemillis ();
Log.info ("Request:" + URL );
Jsonobject jsonobj = NULL;

Httpurlconnection huc = NULL;
Bufferedreader in = NULL;
Inputstreamreader ISR = NULL;
Inputstream is = NULL;

Stringbuffer sb = new stringbuffer (333 );

Try {

URL u = new URL (URL );
Huc = (httpurlconnection) U. openconnection ();
Huc. setrequestmethod ("Post ");
Huc. setdooutput (true );
Huc. getoutputstream (). Write (Params. getbytes ("UTF-8 "));
Huc. getoutputstream (). Flush ();
Huc. getoutputstream (). Close ();
Is = huc. getinputstream ();
ISR = new inputstreamreader (is, "UTF-8 ");
In = new bufferedreader (ISR );
String line = NULL;

While (line = in. Readline ())! = NULL ){
SB. append (line );
}

Long end = system. currenttimemillis ();
Log.info ("response [" + (end-begin) + "]:" + sb. tostring ());
Jsonobj = new jsonobject (sb. tostring ());

} Catch (malformedurlexception e ){
E. printstacktrace ();
} Catch (ioexception e ){
E. printstacktrace ();
} Catch (jsonexception e ){
E. printstacktrace ();
} Finally {
Try {
Huc. Disconnect ();
Is. Close ();
ISR. Close ();
In. Close ();
} Catch (exception e ){
E. printstacktrace ();
}
}

Return jsonobj;
}

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.