Java simuliert das Senden von HTTP -Anfragen. Das erste ist, dass HttpurlConnection Postanfragen sendet. Die zweite ist die Verwendung von HTTPPCLIENT, um Postanfragen zu simulieren.
Beispielcode:
Pakettest; import Java.util.ArrayList; Import Java.util.hashMap; Import Java.util.List; Import Java.util.map; Import Java.util.map.Entry; Import Java.util.Concurent.executorservice; org.apache.http.httpentity; import org.apache.http.namevaluepair; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.entity.urlencodedformentity; org.apache.http.client.methods.cloSeableHttPesponse; import org.apache.http.client.methods.httppost; import org.apache.http.impl.client.closisableHttpclient; import. org.apache.http.message.basicnamevaluepair; import org.apache.http.util.entityUtils;/** Java -Simulationstest MultitHhread -Test http @Author Wdh**/öffentlich -klassen -servicethreadtest runnable {private String -url; url, map <string, Object> paramstr) {Super (); this.url = url; this.paramstr = paramstr;} public String getRl () {return url;} public void seturl (String url) {this.url = url;} öffentliche Karte <String, Object> GetParamstr () {return paramstr. {this.paramstr = paramstr;}@oversidepublic void run () {// http request Implementierungsmethode ClosinableHttpclient httpclient = httpclients.createdefault (); httppost post = new httppost (URL); RequestConfig.custom (). SetConnectionRequestTimeout (10000) .SetConnectTimeOut (10000) .setSockettimeout (10000) .build (); CloseableHtttPesponse -Antwort = null; try {list <namevaluepair> params = sethttpnameValues (paramstr); Httpentity httpentity = new urlencodedformentity (params, "utf-8"); post.setEntity (httpentity); post.setConfig (config); response = httpclient.execute (post); EntityUtils.toString (Entity); System.out.println ("Inhalt:" + Inhalt);} catch (Ausnahme E) {E.printstacktrace ();}} private Liste <nameValuePair> sethttpNamevalues (map <String, Objekt> Parammap) {list <nameVAIL); für (Eintrag <String, Objekt> Eintrag: parammap.EntrySet ()) {params.add (neu BasicNamevaluePair (Eintrag.getKey (), Entry.getValue (). ToString ())); } return params;} public static void main (String [] args) {// Verwenden Sie den Thread -Pool in Java -Werkzeugklasse ExecutorService Pool = Ausführungen Servicetheadest (url, paramstr));}} public static map <String, Objekt> Gethttpparamstr () {map <String, Objekt> param = new Hashmap <String, Object> (); Param.put ("apiversion", 1); param.put ("Appversion", "3.6.2");Das obige Inhalt, die über die HTTP-Anfragen von Java-Simulation mit Multi-Threaded-Anfragen geteilt werden. Vielen Dank für Ihre Unterstützung für Wulin.com.