Java simula las solicitudes de envío HTTP. El primero es httpurlconnection enviando solicitudes de publicación. El segundo es usar httppclient para simular las solicitudes posteriores.
Código de ejemplo:
prueba de paquete; import java.util.arrayList; import java.util.hashmap; import java.util.list; import java.util.map; import java.util.map.entry; import java.util.concurrent.executorservice; import java.util.concurrent.executors; import; importar; importar; importar; import org.apache.http.httpentity; importar org.apache.http.nameValuePair; import org.apache.http.client.config.requestconfig; import org.apache.http.client.entity.urlencoded formentity; importación; org.apache.http.client.methods.closEablehttTpesponse; import org.apache.http.client.methods.httppost; importar org.apache.http.httpclient.ClosableHttpClient; import org.apache.http.Impl.htttpClient; org.apache.http.message.basicNameValuePair; import org.apache.http.util.entityUtils;/** Java Simulación Test Test Http Solicitud*@author wdh**/public Servicethreadtadtest implementsyable {string private map <string, string> paramstr; objects; url, map <string, object> paramstr) {super (); this.url = url; this.paramstr = paramstr;} public string getUrl () {return url;} public void seturl (string url) {this.url = url;} public map <string, objeto> getParamstr () {return paramstr;} public void setparamstr ((mapstr (string, objeto, objeto) {this.paramStr = paramstr;}@overridePublic void run () {// http Método de implementación de solicitudes CierrehttpClient httpClient = httpclients.createDefault (); httppost post = new httpost (url); requestCig config = RequestConfig.custom (). SetConnectionRequestTimeOut (10000) .SetConnectTimeOut (10000) .SetSocketTimeOut (10000) .Build (); cierreHTTTPResponse Response = null; intente {list <nameValuePair> params = sethttpnameValues (paramstr); Httpentity httPentity = new UrlencodedFormentity (params, "UTF-8"); post.SetEntity (httpentity); post.setConfig (config); respuesta = httpclient.exeCute (post); httpentity entity = respuesta.getEntity (); string content = EntityUtils.ToString (entity); system.out.println ("content:" + content);} catch (excepción e) {E.PrintStackTrace ();}} private list <nameValuePair> sethttpNameValues (map <string, object> parammap) {nameValuepair> params = new arraylist <nameVeP for (Entry <String, Object> Entry: ParamMap.EntrySet ()) {params.Add (new BasicNameValuePair (Entry.getKey (), Entry.getValue (). ToString ())); } parámetros de retorno;} public static void main (string [] args) {// use el grupo de hilos en la clase de herramientas java ejecutorservice grupo = ejecutors.newCachedThreadPool (); for (int i = 0; i <2; i ++) {// abre dos hilos string url = "xxxx"; map <string> paramstr = gethtppoRamStr (); ServiceThreadTest (url, paramstr)));}} public static map <String, object> gethtTpParamStr () {map <string, object> param = new HashMap <String, Object> (); param.put ("apiversion", 1); param.put ("appversion", "3.6.2"); return param;}}Lo anterior es todo el contenido compartido sobre las solicitudes HTTP multiproceso de simulación Java. Gracias por su apoyo a Wulin.com.