Thread Pool HTTP Request

Source: Internet
Author: User

Package com.aibi.cmdc.test;


Import Java.io.BufferedReader;
Import Java.io.InputStream;
Import Java.io.InputStreamReader;
Import java.net.HttpURLConnection;
Import Java.net.URL;
Import java.net.URLConnection;
Import Java.util.HashMap;
Import java.util.List;
Import Java.util.Map;
Import Java.util.concurrent.ExecutorService;
Import java.util.concurrent.Executors;
Import Java.util.concurrent.ScheduledExecutorService;
Import Java.util.concurrent.TimeUnit;
Import Java.util.concurrent.atomic.AtomicInteger;


public class Demo {
/**
* Main
* @param args
* @throws Exception
*/
public static void Main (string[] args) throws Exception {
If not set, this key is not set as long as the proxy IP and the proxy port are correct
//System.setproperty ("Http.proxyset", "true");
//System.getproperties (). SetProperty ("Http.proxyhost",
//"Proxy.Om ");
//System.getproperties (). SetProperty ("Http.proxyport", "8080");
//System.out.println (Doget ());
Final String urlnamestring = "Http://webservice.webxml.com.cn/webservices/qqOnlineWebService.asmx/qqCheckOnline?" qqcode=649145636 ";
//String urlnamestring = "Http://www.hao123.com/?tn=39005028_243_hao_pg";
//Httputil.doget (urlnamestring);
//String PostURL = "Http://webservice.webxml.com.cn/webservices/qqOnlineWebService.asmx/qqCheckOnline";
//map<string, string> params = new hashmap<string, string> ();
//Params.put ("Qqcode", "649145636");
//Httputil.dopost (PostURL, params);

//Executorservice Cachedthreadpool = Executors.newcachedthreadpool ();
//for (int i = 0; i < i++) {
//Final int index = i;
//try {
//Thread.Sleep (Index * 1000);
//} catch (Interruptedexception e) {
//E.printstacktrace ();
//}
//Cachedthreadpool.execute (New Runnable () {
//public void Run () {
//SYSTEM.OUT.PRINTLN (index);
//try {
//Httputil.doget (urlnamestring);
//} catch (Exception e) {
//E.printstacktrace ();
//}
//}
//});
//}
Scheduledexecutorservice Scheduledthreadpool = Executors.newscheduledthreadpool (2);
Scheduledthreadpool.scheduleatfixedrate (New Runnable () {

@Override
public void Run () {
try {
Httputil.doget (urlnamestring);
} catch (Exception e) {
E.printstacktrace ();
}
}
}, 1, 3, timeunit.seconds);
}

/**
* Get Request
* @return
* @throws Exception
*/
public static String Doget () throws Exception {
String urlnamestring = "http://webservice.webxml.com.cn/webservices/qqOnlineWebService.asmx/qqCheckOnline?qqCode= 649145636 ";
String result = "";
BufferedReader in = null;
try {
URL realurl = new URL (urlnamestring);
Opening and linking between URLs
URLConnection connection = Realurl.openconnection ();
To set common request properties
Connection.setrequestproperty ("Accept", "*/*");
Connection.setrequestproperty ("Connection", "keep-alive");
Connection.setrequestproperty ("User-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ");
Establish an actual connection
Connection.connect ();
Get all response header fields
map<string, list<string>> map = Connection.getheaderfields ();
Iterate through all the response header fields
For (String Key:map.keySet ()) {
SYSTEM.OUT.PRINTLN (key + "--->" + map.get (key));
}
Defines the response of the BufferedReader input stream to read the URL
in = new BufferedReader (New InputStreamReader (
Connection.getinputstream ()));
String Line;
while (line = In.readline ()) = null) {
result + = line;
}
} catch (Exception e) {
SYSTEM.OUT.PRINTLN ("Send GET request exception!") "+ e);
E.printstacktrace ();
}
Use the finally block to close the input stream
finally {
try {
if (in = null) {
In.close ();
}
} catch (Exception E2) {
E2.printstacktrace ();
}
}
return result;
}
}

Thread Pool HTTP Request

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.