Java HttpURLConnection Interface Call

Source: Internet
Author: User

/**

* @param method is transferred as GET or post
* @param urlstring Basic URL
* @param parameters parameter Map
* @return
* @throws IOException
*/

public string GetToken (String method,string urlstring,map<string, string> parameters) throws ioexception{
HttpURLConnection urlconnection = null;
StringBuffer temp = null;

if (Method.equalsignorecase ("GET") && Parameters! = null) {
StringBuffer param = new StringBuffer ();
int i = 0;
For (String Key:parameters.keySet ()) {
if (i = = 0)
Param.append ("?");
Else
Param.append ("&");
Param.append (Key). Append ("="). Append (Parameters.get (key));
i++;
}
URLString + = param;
}

Establish a connection
URL url = new URL (urlstring);
URLConnection = (httpurlconnection) url.openconnection ();
Setting parameters
Set Connection mode
Urlconnection.setrequestmethod (method);;
Requires output
Urlconnection.setdooutput (TRUE);
Need to enter
Urlconnection.setdoinput (TRUE);
Cache not allowed
Urlconnection.setusecaches (FALSE);

Set request Properties
Urlconnection.setrequestproperty ("Content-type", "application/x-www-form-urlencoded");
Urlconnection.setrequestproperty ("Charset", "UTF-8");
Urlconnection.setconnecttimeout (40000);
Urlconnection.setreadtimeout (40000);
Urlconnection.connect ();

if (Method.equalsignorecase ("POST") && Parameters! = null) {
StringBuffer param = new StringBuffer ();
For (String Key:parameters.keySet ()) {
Param.append ("&");
Param.append (Key). Append ("="). Append (Parameters.get (key));
}
String json=json.tojsonstring (parameters);
Urlconnection.getoutputstream (). Write (Json.tostring (). GetBytes ());
Urlconnection.getoutputstream (). Flush ();
Urlconnection.getoutputstream (). Close ();
}



Get a response
int Resultcode=urlconnection.getresponsecode ();
if (Httpurlconnection.http_ok==resultcode) {
InputStream in = Urlconnection.getinputstream ();
BufferedReader BufferedReader = new BufferedReader (
New InputStreamReader (in));
temp = new StringBuffer ();
String line = Bufferedreader.readline ();
while (line! = null) {
Temp.append (line) append ("\ r \ n");
line = Bufferedreader.readline ();
}
Bufferedreader.close ();
}
if (temp! = null) {
return temp.tostring ();
}else{
return null;
}
}

Java HttpURLConnection Interface Call

Related Article

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.