Http请求两种方式

来源:互联网 发布:java 视频服务器 编辑:程序博客网 时间:2024/05/18 00:40

方式一:

/** * 发送https请求 * * @param requestUrl 请求地址 * @param requestMethod 请求方式(GET、POST) * @param outputStr 提交的数据 * @return 返回微信服务器响应的信息 */@Deprecatedpublic String httpsRequest(String requestUrl, String requestMethod, String outputStr) {    String resContent = null;    DefaultHttpClient httpClient = new DefaultHttpClient ();       try {           // 创建SSLContext对象,并使用我们指定的信任管理器初始化           TrustManager[] tm = { new MyX509TrustManager() };           SSLContext sslContext = SSLContext.getInstance("TLS");           sslContext.init(null, tm, new java.security.SecureRandom());           // 从上述SSLContext对象中得到SSLSocketFactory对象           SSLSocketFactory ssf = new org.apache.http.conn.ssl.SSLSocketFactory(sslContext);           ssf.setHostnameVerifier(new AllowAllHostnameVerifier ());           httpClient.getConnectionManager().getSchemeRegistry().register(new Scheme ("https", ssf, 443));           HttpPost httpGet = new HttpPost (requestUrl);           httpGet.setHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows XP)");           httpGet.setHeader("Content-Type", "application/x-www-form-urlencoded");           httpGet.setEntity(new StringEntity (outputStr, "UTF-8"));           HttpResponse response = httpClient.execute(httpGet);           if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {               HttpEntity httpEntity = response.getEntity();               if (httpEntity != null) {                   resContent = EntityUtils.toString(httpEntity);// 取出应答字符串                   resContent = new String(resContent.getBytes("ISO-8859-1"), "UTF-8");                   httpEntity.consumeContent();               }           } else {               httpGet.abort();           }       } catch (Exception e) {           log.error("连接超时:{}", e);       } finally {           httpClient.getConnectionManager().shutdown();       }    return resContent;}

方式二:

/**  * 请求,只请求一次,不做重试   *   * @param url   请求的地址   * @param data  提交的数据   * @param connectTimeoutMs 连接超时时间   * @param readTimeoutMs 读取超时时间   * @return   * @throws Exception   */  public String requestOnce(String url, String data, int connectTimeoutMs, int readTimeoutMs) throws Exception {      BasicHttpClientConnectionManager connManager;      //设置连接管理器,BasicHttpClientConnectionManager对象线程安全,每次只管理一个连接      connManager = new BasicHttpClientConnectionManager (              RegistryBuilder.<ConnectionSocketFactory>create()                      .register("http", PlainConnectionSocketFactory.getSocketFactory())                      .register("https", SSLConnectionSocketFactory.getSocketFactory())                      .build(),              null,              null,              null      );      //构建客户端      HttpClient httpClient = HttpClientBuilder.create()              .setConnectionManager(connManager)              .build();      HttpPost httpPost = new HttpPost(url);      //设置请求和传输超时时间      RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(readTimeoutMs).setConnectTimeout(connectTimeoutMs).build();      httpPost.setConfig(requestConfig);      //设置请求头和请求数据编码格式      StringEntity postEntity = new StringEntity(data, "UTF-8");      httpPost.addHeader("Content-Type", "text/xml");      httpPost.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows XP)");      httpPost.setEntity(postEntity);      //发送请求,接收请求数据,将请求数据转为string      HttpResponse httpResponse = httpClient.execute(httpPost);      HttpEntity httpEntity = httpResponse.getEntity();      return EntityUtils.toString(httpEntity, "UTF-8");  }