public RestaurantRestClient(String urlStub) {
   httpClient.getHttpConnectionManager().getParams().setMaxTotalConnections(10);
   httpClient
       .getHttpConnectionManager()
       .getParams()
       .setMaxConnectionsPerHost(httpClient.getHostConfiguration(), 10);
   restClient.setErrorHandler(new MyErrorHandler());
   this.urlStub = urlStub;
   //     List<HttpMessageConverter<?>> converters = new ArrayList<>();
   //    converters.add(converter);
   //     restClient.setMessageConverters(converters);
   // http://localhost:8888/app/backbone/restaurant/
 }
示例#2
0
  public String getUrl(
      String url, Map<String, String> parameters, String encoding, int millisecond) {

    String result = "";
    GetMethod method = null;
    try {
      // 设置参数
      if (parameters != null) {
        StringBuilder params = new StringBuilder();
        boolean isFirst = true;
        for (Map.Entry<String, String> entry : parameters.entrySet()) {
          if (isFirst) {
            params.append("?");
          } else {
            params.append("&");
          }
          params.append(entry.getKey() + "=" + entry.getValue());
          isFirst = false;
        }
        url = url + params;
      }

      method = new GetMethod(url);
      httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(millisecond);
      httpClient.getHttpConnectionManager().getParams().setSoTimeout(millisecond);
      // add by aidi -start 增大httpclient链接池大小
      httpClient.getHttpConnectionManager().getParams().setDefaultMaxConnectionsPerHost(512);
      httpClient.getHttpConnectionManager().getParams().setMaxTotalConnections(1024);
      // add by aidi -end
      method
          .getParams()
          .setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, true));
      method.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, encoding);
      // 访问url失败,返回空结果
      if (httpClient.executeMethod(method) != HttpStatus.SC_OK) {
        return result;
      }

      // 将返回的http响应传入解析方法进行解析
      result = method.getResponseBodyAsString();
      return result;
    } catch (Exception ex) {
      logger.error("访问url失败[url=" + url + "]", ex);
      throw new RuntimeException("访问url失败[url=" + url + "]", ex);
    } finally {
      if (method != null) {
        method.releaseConnection();
      }
    }
  }
  public String getURL(String url, String username, String pw) throws MalformedURLException {

    GetMethod httpMethod = null;
    try {
      HttpClient client = new HttpClient();
      setAuth(client, url, username, pw);
      httpMethod = new GetMethod(url);
      client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
      int status = client.executeMethod(httpMethod);
      if (status == HttpStatus.SC_OK) {
        InputStream is = httpMethod.getResponseBodyAsStream();
        String response = IOUtils.toString(is);
        if (response.trim().length() == 0) { // sometime gs rest fails
          logger.warn("ResponseBody is empty");
          return null;
        } else {
          return response;
        }
      } else {
        logger.info("(" + status + ") " + HttpStatus.getStatusText(status) + " -- " + url);
      }
    } catch (ConnectException e) {
      logger.info("Couldn't connect to [" + url + "]");
    } catch (IOException e) {
      logger.info("Error talking to [" + url + "]", e);
    } finally {
      if (httpMethod != null) httpMethod.releaseConnection();
    }

    return null;
  }
示例#4
0
 private static HttpClient getHttpClient() {
   HttpClient httpClient = new HttpClient();
   // 设置 HttpClient 接收 Cookie,用与浏览器一样的策略
   httpClient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
   // 设置 默认的超时重试处理策略
   httpClient
       .getParams()
       .setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
   // 设置 连接超时时间
   httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(1000);
   // 设置 读数据超时时间
   httpClient.getHttpConnectionManager().getParams().setSoTimeout(1000);
   // 设置 字符集
   httpClient.getParams().setContentCharset("UTF-8");
   return httpClient;
 }
示例#5
0
  public String mediaCreate(File f, DCArray assets_names, DCObject meta) throws Exception {
    String upload_url = this.fileUpload();

    PostMethod filePost = null;
    try {
      filePost = new PostMethod(upload_url);

      Part[] parts = {new FilePart("file", f)};

      filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost.getParams()));
      HttpClient client = new HttpClient();
      client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);

      int status = client.executeMethod(filePost);
      if (status == HttpStatus.SC_OK) {
        ObjectMapper mapper = new ObjectMapper();
        DCObject json_response =
            DCObject.create(mapper.readValue(filePost.getResponseBodyAsString(), Map.class));
        return this.mediaCreate(json_response.pull("url"), assets_names, meta);
      } else {
        throw new DCException("Upload failed.");
      }
    } catch (Exception e) {
      throw new DCException("Upload failed: " + e.getMessage());
    } finally {
      if (filePost != null) {
        filePost.releaseConnection();
      }
    }
  }
示例#6
0
 private boolean checkURIExists(String uriFile) throws IOException, HttpException {
   int returnCode = 0;
   GetMethod propFind = new GetMethod(uriFile);
   returnCode = davClient.executeMethod(propFind);
   davClient.getHttpConnectionManager().closeIdleConnections(0);
   return returnCode == 200;
 }
  @NotNull
  private static HttpClient getHttpClient(
      @Nullable final String login, @Nullable final String password) {
    final HttpClient client = new HttpClient();
    HttpConnectionManagerParams params = client.getHttpConnectionManager().getParams();
    params.setConnectionTimeout(
        CONNECTION_TIMEOUT); // set connection timeout (how long it takes to connect to remote host)
    params.setSoTimeout(
        CONNECTION_TIMEOUT); // set socket timeout (how long it takes to retrieve data from remote
                             // host)

    client.getParams().setContentCharset("UTF-8");
    // Configure proxySettings if it is required
    final HttpConfigurable proxySettings = HttpConfigurable.getInstance();
    if (proxySettings.USE_HTTP_PROXY && !StringUtil.isEmptyOrSpaces(proxySettings.PROXY_HOST)) {
      client.getHostConfiguration().setProxy(proxySettings.PROXY_HOST, proxySettings.PROXY_PORT);
      if (proxySettings.PROXY_AUTHENTICATION) {
        client
            .getState()
            .setProxyCredentials(
                AuthScope.ANY,
                new UsernamePasswordCredentials(
                    proxySettings.PROXY_LOGIN, proxySettings.getPlainProxyPassword()));
      }
    }
    if (login != null && password != null) {
      client.getParams().setCredentialCharset("UTF-8");
      client.getParams().setAuthenticationPreemptive(true);
      client
          .getState()
          .setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(login, password));
    }
    return client;
  }
示例#8
0
  /**
   * 单次上传图片or文件
   *
   * @param path
   * @param name
   * @param filePath
   * @return
   * @throws Exception
   */
  public static String sendDataByHttpClientPost(String path, String name, String filePath)
      throws Exception {

    /*
     * List<Part> partList = new ArrayList<Part>(); partList.add(new
     * StringPart("user", name));
     *
     * for (int i = 0; i < 4; i++) { partList.add(new FilePart(name,
     * FilePart())); }
     */

    // 实例化上传数据的数组
    Part[] parts = {new StringPart("user", name), new FilePart("file", new File(filePath))};

    PostMethod filePost = new PostMethod(path);

    filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost.getParams()));

    org.apache.commons.httpclient.HttpClient client =
        new org.apache.commons.httpclient.HttpClient();
    client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);

    int status = client.executeMethod(filePost);
    Log.e("Other", "返回结果:" + status);
    if (status == 200) {

      System.out.println(filePost.getResponseCharSet());
      String result = new String(filePost.getResponseBodyAsString());

      System.out.println("--" + result);
      return result;
    } else {
      throw new IllegalStateException("服务器状态异常");
    }
  }
示例#9
0
 /** Make sure we can get a valid http client. */
 @Test
 public void canGetHttpClient() {
   final HttpClient client = this.config.httpClient();
   Assert.assertNotNull(client);
   Assert.assertTrue(
       client.getHttpConnectionManager() instanceof MultiThreadedHttpConnectionManager);
 }
示例#10
0
    public HttpClientFrame() {
      client = new HttpClient(new MultiThreadedHttpConnectionManager());
      client.getHttpConnectionManager().getParams().setConnectionTimeout(30000);

      JPanel panInput = new JPanel(new FlowLayout());

      String[] aURLs = {
        "http://www.apache.org/",
        "http://www.google.com/",
        "http://www.opensource.org/",
        "http://www.anybrowser.org/",
        "http://jakarta.apache.org/",
        "http://www.w3.org/"
      };

      final JButton btnGET = new JButton("GET");
      btnGET.addActionListener(
          new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
              String url = (String) cmbURL.getSelectedItem();
              if (url != null && url.length() > 0) {
                loadPage(url);
              }
            }
          });

      cmbURL = new JComboBox(aURLs);
      cmbURL.setToolTipText("Enter a URL");
      cmbURL.setEditable(true);
      cmbURL.setSelectedIndex(0);

      JLabel lblURL = new JLabel("URL:");

      panInput.add(lblURL);
      panInput.add(cmbURL);
      panInput.add(btnGET);

      taTextResponse = new JTextArea();
      taTextResponse.setEditable(false);
      taTextResponse.setCaretPosition(0);

      htmlPane = new JEditorPane();
      htmlPane.setContentType("text/html");
      htmlPane.setEditable(false);

      JSplitPane splitResponsePane =
          new JSplitPane(
              JSplitPane.HORIZONTAL_SPLIT,
              new JScrollPane(taTextResponse),
              new JScrollPane(htmlPane));
      splitResponsePane.setOneTouchExpandable(false);
      splitResponsePane.setDividerLocation(350);
      // it would be better to set resizeWeight, but this method does
      // not exist in JRE 1.2.2
      //            splitResponsePane.setResizeWeight(0.5);

      this.getContentPane().setLayout(new BorderLayout());
      this.getContentPane().add(panInput, BorderLayout.NORTH);
      this.getContentPane().add(splitResponsePane, BorderLayout.CENTER);
    }
示例#11
0
  public void httpOnline() throws HttpException, IOException {
    /* 1 生成 HttpClinet 对象并设置参数*/
    HttpClient httpClient = new HttpClient();
    // 设置 Http 连接超时为5秒
    httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
    /*2 生成 GetMethod 对象并设置参数*/
    GetMethod getMethod = new GetMethod(m_ipAddr);
    // 设置 get 请求超时为 5 秒
    getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 5000);
    // 设置请求重试处理,用的是默认的重试处理:请求三次
    getMethod
        .getParams()
        .setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
    /*3 执行 HTTP GET 请求*/

    long startTime = System.currentTimeMillis();
    if (HttpStatus.SC_OK == httpClient.executeMethod(getMethod)) {
      statusCode = "Up";
      responseTime = System.currentTimeMillis() - startTime;
    } else {
      statusCode = "Down";
      responseTime = -1;
    }

    /*6 .释放连接*/
    getMethod.releaseConnection();
  }
示例#12
0
  /**
   * 批量上传图片
   *
   * @param path 服务器地址
   * @param name 用户名
   * @param filePath sd卡图片路径
   * @param onSuccessListner
   * @return
   * @throws Exception
   */
  public static String sendDataByHttpClientPost(
      String path, List<File> files, List<Part> mparameters, OnSuccessListner listner)
      throws Exception {

    for (int i = 0; i < files.size(); i++) {
      mparameters.add(new FilePart("file", files.get(i)));
    }
    Part[] parts = mparameters.toArray(new Part[0]);

    PostMethod filePost = new PostMethod(path);
    filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost.getParams()));
    HttpClient client = new HttpClient();
    client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);

    int status = client.executeMethod(filePost);
    if (status == 200) {
      Log.e("DataService", "" + filePost.getResponseCharSet());
      String result = new String(filePost.getResponseBodyAsString());
      Log.e("DataService", "--" + result);
      // JSONArray array = (JSONArray) JSON.parse(result);
      Log.e("JSONArray", "--" + result);
      listner.onSuccess(result);
      return result;
    } else {
      listner.onFailed();
      return null;
    }
  }
  /**
   * 发上服务器的文件
   *
   * @param file
   * @return
   */
  private String sendHttpRequest(File file) {
    String string = null;
    PostMethod filePost = new PostMethod(targetURL);
    try {
      Part[] parts = {
        new FilePart("userFile.file", targetFile), new StringPart(paramType, param, "utf-8")
      };
      HttpMethodParams params = filePost.getParams();
      params.setContentCharset("utf-8");
      filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost.getParams()));
      httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(50000);

      Log.v("filePost", filePost.toString());
      Log.v("param", param);
      Log.v("paramType", paramType);
      int status = httpClient.executeMethod(filePost);
      // TODO
      Log.v("status--->", status + "");

      InputStream in = filePost.getResponseBodyAsStream();
      byte[] readStream = readStream(in);
      string = new String(readStream);
    } catch (Exception ex) {
      ex.printStackTrace();
    } finally {
      filePost.releaseConnection();
    }
    return string;
  }
示例#14
0
  /**
   * Sends a new HTTP request to a server through a proxy.
   *
   * @param requestParameters a set of parameters that will set the content of the request and
   *     specify the proxy it should go through
   */
  public void callWilmaTestServer(
      final RequestParameters requestParameters, final TestClientParameters clientParameters) {
    try {
      HttpClient httpClient = new HttpClient();
      PostMethod httpPost = new PostMethod(requestParameters.getTestServerUrl());
      if (clientParameters.isUseProxy()) {
        httpClient
            .getHostConfiguration()
            .setProxy(requestParameters.getWilmaHost(), requestParameters.getWilmaPort());
      }
      InputStream inputStream = requestParameters.getInputStream();
      if (requestParameters.getContentType().contains("fastinfoset")) {
        inputStream = compress(inputStream);
      }
      if (requestParameters.getContentEncoding().contains("gzip")) {
        inputStream = encode(inputStream);
        httpPost.setRequestHeader("Content-Encoding", requestParameters.getContentEncoding());
      }
      InputStreamRequestEntity entity =
          new InputStreamRequestEntity(inputStream, requestParameters.getContentType());
      httpPost.setRequestEntity(entity);
      httpPost.setRequestHeader("Accept", requestParameters.getAcceptHeader());
      httpPost.addRequestHeader("Accept-Encoding", requestParameters.getAcceptEncoding());
      // httpPost.addRequestHeader("0", "WilmaBypass=true");

      httpClient
          .getHttpConnectionManager()
          .getParams()
          .setSendBufferSize(clientParameters.getRequestBufferSize());
      httpClient
          .getHttpConnectionManager()
          .getParams()
          .setReceiveBufferSize(clientParameters.getResponseBufferSize());

      int statusCode = httpClient.executeMethod(httpPost);
      logger.info("status code: " + statusCode);
      if (clientParameters.getAllowResponseLogging()) {
        logger.info(getInputStreamAsString(httpPost.getResponseBodyAsStream()));
      }
    } catch (UnsupportedEncodingException e) {
      throw new SystemException("Unsupported encoding.", e);
    } catch (HttpException e) {
      throw new SystemException("Http exception occurred.", e);
    } catch (IOException e) {
      throw new SystemException("InputStream cannot be read.", e);
    }
  }
 public HttpProxy() {
   httpClient = new HttpClient(new MultiThreadedHttpConnectionManager());
   httpClient.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
   httpClient.getHttpConnectionManager().getParams().setSoTimeout(60 * 1000);
   httpClient
       .getParams()
       .setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(0, false));
 }
示例#16
0
  @Override
  public long getTargetVersion(PublishingTargetItem target, String site) {
    long version = -1;
    if (target.getVersionUrl() != null && !target.getVersionUrl().isEmpty()) {
      LOGGER.debug(String.format("Get deployment agent version for target ", target.getName()));
      URL versionUrl = null;
      try {
        versionUrl = new URL(target.getVersionUrl());
      } catch (MalformedURLException e) {
        LOGGER.error(String.format("Invalid get version URL for target [%s]", target.getName()), e);
      }
      GetMethod getMethod = null;
      HttpClient client = null;
      try {
        getMethod = new GetMethod(target.getVersionUrl());
        String siteId = target.getSiteId();
        if (StringUtils.isEmpty(siteId)) {
          siteId = site;
        }
        getMethod.setQueryString(
            new NameValuePair[] {
              new NameValuePair(TARGET_REQUEST_PARAMETER, target.getTarget()),
              new NameValuePair(SITE_REQUEST_PARAMETER, siteId)
            });
        client = new HttpClient();
        int status = client.executeMethod(getMethod);
        if (status == HttpStatus.SC_OK) {
          String responseText = getMethod.getResponseBodyAsString();
          if (responseText != null && !responseText.isEmpty()) {
            version = Long.parseLong(responseText.trim());
          } else {
            version = 0;
          }
        }

      } catch (Exception e) {
        // LOGGER.error(String.format("Target (%s) responded with error while checking target
        // version. Get version failed for url %s", target.getName(), target.getVersionUrl()));

      } finally {
        if (client != null) {
          HttpConnectionManager mgr = client.getHttpConnectionManager();
          if (mgr instanceof SimpleHttpConnectionManager) {
            ((SimpleHttpConnectionManager) mgr).shutdown();
          }
        }
        if (getMethod != null) {
          getMethod.releaseConnection();
        }
        getMethod = null;
        client = null;
      }
    }
    return version;
  }
  private String sendRequest(TicketsRequest request) throws HttpException, IOException {

    String html = "";
    GetMethod get = new GetMethod(url2);
    client.executeMethod(get);

    int statusCodeInit = client.executeMethod(get);
    if (statusCodeInit != HttpStatus.SC_OK) {
      logger.error("sendRequest method failed. Get method failed: " + get.getStatusLine());
      return null;
    } else {
      html = IOUtils.toString(get.getResponseBodyAsStream(), "UTF-8");
    }
    parseToken(html);

    client.getHttpConnectionManager().getParams().setSoTimeout(10000);
    PostMethod post = new PostMethod(URL);

    post.addRequestHeader("Accept", "*/*");
    post.addRequestHeader("Accept-Encoding", "gzip, deflate");
    post.addRequestHeader("Accept-Language", "uk,ru;q=0.8,en-US;q=0.5,en;q=0.3");
    //		post.addRequestHeader("Cache-Control", "no-cache");
    post.addRequestHeader("Connection", "keep-alive");
    //		post.addRequestHeader("Content-Length", "288"); //202. 196
    post.addRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
    post.addRequestHeader("GV-Ajax", "1");
    post.addRequestHeader("GV-Referer", "http://booking.uz.gov.ua/");
    post.addRequestHeader("GV-Screen", "1920x1080");
    post.addRequestHeader("GV-Token", token);
    post.addRequestHeader("GV-Unique-Host", "1");
    post.addRequestHeader("Host", "booking.uz.gov.ua");
    //		post.addRequestHeader("Pragma", "no-cache");
    post.addRequestHeader("Origin", "http://booking.uz.gov.ua");
    post.addRequestHeader("Referer", "http://booking.uz.gov.ua/");
    post.addRequestHeader(
        "User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:43.0) Gecko/20100101 Firefox/39.0");

    post.addParameter("another_ec", "0");
    post.addParameter("date_dep", new SimpleDateFormat("dd.MM.yyyy").format(request.date));
    post.addParameter("search", "");
    post.addParameter("station_from", request.from.getName());
    post.addParameter("station_id_from", request.from.getStationId());
    post.addParameter("station_id_till", request.till.getStationId());
    post.addParameter("station_till", request.till.getName());
    post.addParameter("time_dep", "00:00");
    post.addParameter("time_dep_till", "");

    int statusCode = client.executeMethod(post);
    if (statusCode != HttpStatus.SC_OK) {
      logger.error("sendRequest method failed. Post method failed: " + post.getStatusLine());
      return null;
    }

    return post.getResponseBodyAsString();
  }
示例#18
0
  @Override
  public long setTargetVersion(PublishingTargetItem target, long newVersion, String site) {
    long resoponseVersion = -1;
    if (target.getVersionUrl() != null && !target.getVersionUrl().isEmpty()) {
      LOGGER.debug("Set deployment agent version for target {0}", target.getName());
      URL versionUrl = null;
      try {
        versionUrl = new URL(target.getVersionUrl());
      } catch (MalformedURLException e) {
        LOGGER.error("Invalid set version URL for target [%s]", target.getName());
        return resoponseVersion;
      }
      PostMethod postMethod = null;
      HttpClient client = null;
      try {
        postMethod = new PostMethod(target.getVersionUrl());
        postMethod.addParameter(TARGET_REQUEST_PARAMETER, target.getTarget());
        postMethod.addParameter(VERSION_REQUEST_PARAMETER, String.valueOf(newVersion));
        String siteId = target.getSiteId();
        if (StringUtils.isEmpty(siteId)) {
          siteId = site;
        }
        postMethod.addParameter(SITE_REQUEST_PARAMETER, site);
        client = new HttpClient();
        int status = client.executeMethod(postMethod);
        if (status == HttpStatus.SC_OK) {
          String responseText = postMethod.getResponseBodyAsString();
          if (responseText != null && !responseText.isEmpty()) {
            resoponseVersion = Long.parseLong(responseText);
          } else {
            resoponseVersion = 0;
          }
        }

      } catch (Exception e) {
        LOGGER.error(
            "Target {0} responded with error while setting target version. Set version failed for url {1}",
            target.getName(), target.getVersionUrl());

      } finally {
        if (client != null) {
          HttpConnectionManager mgr = client.getHttpConnectionManager();
          if (mgr instanceof SimpleHttpConnectionManager) {
            ((SimpleHttpConnectionManager) mgr).shutdown();
          }
        }
        if (postMethod != null) {
          postMethod.releaseConnection();
        }
        postMethod = null;
        client = null;
      }
    }
    return resoponseVersion;
  }
示例#19
0
 private HttpClient createHttpClient() {
   HttpClient client = new HttpClient();
   client.getHttpConnectionManager().getParams().setConnectionTimeout(timeout);
   client.getParams().setConnectionManagerTimeout(timeout);
   if (proxy) {
     HostConfiguration hcf = new HostConfiguration();
     hcf.setProxy(this.proxyUrl, this.proxyPort);
     client.setHostConfiguration(hcf);
   }
   return client;
 }
示例#20
0
  public void testConfigureClient() throws Exception {
    WebLocation location = new WebLocation(TestUrl.DEFAULT.getHttpOk().toString());

    WebUtil.createHostConfiguration(client, location, null /*monitor*/);

    HttpConnectionManagerParams params = client.getHttpConnectionManager().getParams();
    assertEquals(
        CoreUtil.TEST_MODE ? 2 : MAX_HTTP_HOST_CONNECTIONS_DEFAULT,
        params.getMaxConnectionsPerHost(HostConfiguration.ANY_HOST_CONFIGURATION));
    assertEquals(
        CoreUtil.TEST_MODE ? 20 : MAX_HTTP_TOTAL_CONNECTIONS_DEFAULT,
        params.getMaxTotalConnections());
  }
 /**
  * Get an instance of the HTTP client to use.
  *
  * @return HTTP client initialized with admin credentials
  */
 protected static HttpClient getClient() {
   if (AbstractEscapingTest.client == null) {
     HttpClient adminClient = new HttpClient();
     Credentials defaultcreds = new UsernamePasswordCredentials("Admin", "admin");
     adminClient.getState().setCredentials(AuthScope.ANY, defaultcreds);
     HttpClientParams clientParams = new HttpClientParams();
     clientParams.setSoTimeout(2000);
     HttpConnectionManagerParams connectionParams = new HttpConnectionManagerParams();
     connectionParams.setConnectionTimeout(30000);
     adminClient.getHttpConnectionManager().setParams(connectionParams);
     AbstractEscapingTest.client = adminClient;
   }
   return AbstractEscapingTest.client;
 }
示例#22
0
  /**
   * 构造Http客户端对象 <功能详细描述>
   *
   * @return [参数说明]
   * @return HttpClient [返回类型说明]
   * @exception throws [违例类型] [违例说明]
   * @see [类、类#方法、类#成员]
   */
  public HttpClient getHttpClient() {

    /** 链接的超时数,默认为5秒,此处要做成可配置 */
    final int CONNECTION_TIME_OUT = 5000;
    final int SOCKET_TIME_OUT = 5000;

    /** 每个主机的最大并行链接数,默认为10 */
    final int MAX_CONNECTIONS_PER_HOST = 10;

    /** 客户端总并行链接最大数,默认为50 */
    final int MAX_TOTAL_CONNECTIONS = 50;

    // 此处运用连接池技术。
    MultiThreadedHttpConnectionManager manager = new MultiThreadedHttpConnectionManager();

    // 设定参数:与每个主机的最大连接数
    manager.getParams().setDefaultMaxConnectionsPerHost(MAX_CONNECTIONS_PER_HOST);

    // 设定参数:客户端的总连接数
    manager.getParams().setMaxTotalConnections(MAX_TOTAL_CONNECTIONS);

    // 使用连接池技术创建HttpClient对象
    HttpClient httpClient = new HttpClient(manager);

    // 设置超时时间
    httpClient.getParams().setConnectionManagerTimeout(CONNECTION_TIME_OUT); // 从连接池中获取连接超时设置
    httpClient
        .getHttpConnectionManager()
        .getParams()
        .setConnectionTimeout(CONNECTION_TIME_OUT); // 建立连接超时设置
    httpClient
        .getHttpConnectionManager()
        .getParams()
        .setSoTimeout(SOCKET_TIME_OUT); // socket上没有数据流动超时设置

    return httpClient;
  }
  @Override
  public void init(ServletConfig config) throws ServletException {
    super.init(config);

    httpClient = new HttpClient(new MultiThreadedHttpConnectionManager());
    httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
    /* proxy is not needed here I think, but I'll keep this just in case
    String proxyHost = System.getProperty("http.proxyHost");
    String proxyPort = System.getProperty("http.proxyPort");
    logger.info("Checking system properties for proxy configuration: [{}:{}]", proxyHost, proxyPort);
    if (null != proxyHost && null != proxyPort) {
        httpClient.getHostConfiguration().setProxy(proxyHost, Integer.parseInt(proxyPort));
    }
    */
  }
 /**
  * @param sProxyUser
  * @param sProxyPassswd
  * @return an HTTP client
  */
 private static HttpClient getHTTPClient(
     String sProxyUser, String sProxyPassswd, int iConTimeout, int iDataTimeout) {
   HttpClient client = new HttpClient();
   client
       .getHttpConnectionManager()
       .getParams()
       .setConnectionTimeout(iConTimeout); // connection to
   client
       .getHttpConnectionManager()
       .getParams()
       .setSoTimeout(iDataTimeout); // data reception timeout
   if (sProxyUser != null && sProxyPassswd != null) {
     client
         .getHostConfiguration()
         .setProxy(
             ConfigurationManager.getProperty(CONF_NETWORK_PROXY_HOSTNAME),
             Integer.parseInt(ConfigurationManager.getProperty(CONF_NETWORK_PROXY_PORT)));
     client
         .getState()
         .setProxyCredentials(
             new AuthScope(AuthScope.ANY), new UsernamePasswordCredentials(sProxyUser, sProxyPwd));
   }
   return client;
 }
示例#25
0
  public static String HttpPostForLogistics(String url, String method, String str) {
    String encoding = "UTF-8";
    String webUrl = url + "/" + method;
    StringBuffer sBuffer = new StringBuffer();
    // 构造HttpClient的实例
    HttpClient httpClient = new HttpClient();
    // 创建POS方法的实例
    PostMethod postMethod = new PostMethod(webUrl);
    try {
      // postMethod.setRequestEntity(new ByteArrayRequestEntity(str.getBytes(),"application/json;
      // charset=utf-8"));
      postMethod.setRequestBody(str);
    } catch (Exception e1) {
      e1.printStackTrace();
    }
    postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, encoding);
    httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(10000); // 连接5秒超时
    httpClient.getHttpConnectionManager().getParams().setSoTimeout(30000); // 读取30秒超时
    // postMethod.setRequestHeader("Content-type", "application/json; charset=utf-8");
    // postMethod.setDoAuthentication(false);
    // postMethod.addRequestHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT
    // 5.1)");

    postMethod.setDoAuthentication(true);
    postMethod.addRequestHeader("Authorization", "Basic U0FQX0FNOkFubmllTWFvMTIzNA==");
    postMethod.setRequestHeader("Content-type", "application/json");
    try {
      // 执行getMethod
      int statusCode = httpClient.executeMethod(postMethod);
      if (statusCode != HttpStatus.SC_OK) {
        System.err.println("Method failed: " + postMethod.getStatusLine());
        sBuffer = new StringBuffer();
      } else {
        sBuffer = new StringBuffer(postMethod.getResponseBodyAsString() + "");
      }
    } catch (HttpException e) {
      // 发生致命的异常,可能是协议不对或者返回的内容有问题
      System.out.println("Please check your provided http address!");
      e.printStackTrace();
    } catch (IOException e) {
      // 发生网络异常
      e.printStackTrace();
    } finally {
      // 释放连接
      postMethod.releaseConnection();
    }
    return sBuffer.toString();
  }
  /* 下载 url 指向的网页 */
  public String downloadFile(String url) {
    String filePath = null;
    /* 1.生成 HttpClinet 对象并设置参数 */
    HttpClient httpClient = new HttpClient();
    // 设置 Http 连接超时 5s
    httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(5000);

    /* 2.生成 GetMethod 对象并设置参数 */
    GetMethod getMethod = new GetMethod(url);
    // 设置 get 请求超时 5s
    getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 5000);
    // 设置请求重试处理
    getMethod
        .getParams()
        .setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());

    /* 3.执行 HTTP GET 请求 */
    try {
      int statusCode = httpClient.executeMethod(getMethod);
      // 判断访问的状态码
      if (statusCode != HttpStatus.SC_OK) {
        System.err.println("Method failed: " + getMethod.getStatusLine());
        filePath = null;
      }

      /* 4.处理 HTTP 响应内容 */
      byte[] responseBody = getMethod.getResponseBody(); // 读取为字节数组
      // 根据网页 url 生成保存时的文件名
      filePath =
          "temp\\" + getFileNameByUrl(url, getMethod.getResponseHeader("Content-Type").getValue());
      // createFile(filePath);
      FileUtils.createFile(filePath);
      saveToLocal(responseBody, filePath, false);
    } catch (HttpException e) {
      // 发生致命的异常,可能是协议不对或者返回的内容有问题
      System.out.println("Please check your provided http address!");
      e.printStackTrace();
    } catch (IOException e) {
      // 发生网络异常
      e.printStackTrace();
    } finally {
      // 释放连接
      getMethod.releaseConnection();
    }
    return filePath;
  }
示例#27
0
  @Override
  public boolean checkConnection(PublishingTargetItem target) {
    boolean connOk = false;
    if (target.getStatusUrl() != null && !target.getStatusUrl().isEmpty()) {
      LOGGER.debug(String.format("Check deployment agent status for target ", target.getName()));
      URL statusUrl = null;
      try {
        statusUrl = new URL(target.getStatusUrl());
      } catch (MalformedURLException e) {
        LOGGER.error(
            String.format(
                "Invalid endpoint status URL for publishing channel [%s]", target.getName()),
            e);
      }
      GetMethod getMethod = null;
      HttpClient client = null;
      try {
        getMethod = new GetMethod(target.getStatusUrl());
        client = new HttpClient();
        int status = client.executeMethod(getMethod);
        if (status == HttpStatus.SC_OK) {
          connOk = true;
        }

      } catch (Exception e) {
        LOGGER.error(
            String.format(
                "Target (%s) is not available. Status check failed for url %s",
                target.getName(), target.getStatusUrl()));
      } finally {
        if (client != null) {
          HttpConnectionManager mgr = client.getHttpConnectionManager();
          if (mgr instanceof SimpleHttpConnectionManager) {
            ((SimpleHttpConnectionManager) mgr).shutdown();
          }
        }
        if (getMethod != null) {
          getMethod.releaseConnection();
        }
        getMethod = null;
        client = null;
      }
    }
    return connOk;
  }
示例#28
0
 // ���� URL ָ�����ҳ
 public String downloadFile(String url) {
   String filePath = null;
   // 1.���� HttpClinet��������
   HttpClient httpClient = new HttpClient();
   // ���� HTTP���ӳ�ʱ 5s
   httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
   // 2.���� GetMethod��������
   GetMethod getMethod = new GetMethod(url);
   // ���� get����ʱ 5s
   getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 5000);
   // �����������Դ���
   getMethod
       .getParams()
       .setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
   // 3.ִ��GET����
   try {
     int statusCode = httpClient.executeMethod(getMethod);
     // �жϷ��ʵ�״̬��
     if (statusCode != HttpStatus.SC_OK) {
       System.err.println("Method failed: " + getMethod.getStatusLine());
       filePath = null;
     }
     // 4.���� HTTP ��Ӧ����
     byte[] responseBody = getMethod.getResponseBody(); // ��ȡΪ�ֽ�����
     // ������ҳ url ���ɱ���ʱ���ļ���
     filePath =
         "temp\\" + getFileNameByUrl(url, getMethod.getResponseHeader("Content-Type").getValue());
     File file = new File(filePath);
     if (!file.getParentFile().exists()) {
       file.getParentFile().mkdir();
     }
     saveToLocal(responseBody, filePath);
   } catch (HttpException e) {
     // �����������쳣��������Э�鲻�Ի��߷��ص�����������
     System.out.println("�������http��ַ�Ƿ���ȷ");
     e.printStackTrace();
   } catch (IOException e) {
     // ���������쳣
     e.printStackTrace();
   } finally {
     // �ͷ�����
     getMethod.releaseConnection();
   }
   return filePath;
 }
示例#29
0
  @Test
  public void docDelete() throws UnsupportedEncodingException {

    String api_url = "";

    // httpClient
    HttpClient httpClient = new HttpClient();
    httpClient.getParams().setSoTimeout(6000);
    httpClient.getParams().setVersion(HttpVersion.HTTP_1_0);
    httpClient.getParams().setCredentialCharset("UTF-8");
    httpClient.getParams().setContentCharset("UTF-8");
    httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(6000);

    PostMethod postMethod = new PostMethod(api_url);
    postMethod.addRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    postMethod.addParameter("from", "docquality"); // API 호출하는 운영툴 명칭
    postMethod.addParameter("admin_id", "ju0805"); // 삭제요청을 한 운영자 id
    postMethod.addParameter("query", "콜걸"); // 검색쿼리
    postMethod.addParameter("srs", "off"); // 원문삭제여부
    postMethod.addParameter("service", "blog"); // 서비스명
    postMethod.addParameter("description", "불법"); // 부가코멘트
    postMethod.addParameter("action_code", "10401"); // 액션코드
    postMethod.addParameter("type", "article"); // 타입
    postMethod.addParameter("reason1", "ILLEGAL");
    postMethod.addParameter("docurl", "http://blog.daum.net/jjipeter/4");
    postMethod.addParameter("docid", "1799508185");
    postMethod.addParameter("dsid", "22YGTLTRZlm6NVoLDs");

    try {
      int resCode = httpClient.executeMethod(postMethod);
      if (resCode == HttpStatus.SC_OK) {
        JSONObject jsonObejct = new JSONObject();
        String contents = postMethod.getResponseBodyAsString();
        jsonObejct = JSONObject.fromObject(contents);
        System.out.println(jsonObejct);
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      postMethod.releaseConnection();
    }
  }
  /**
   * 根据url获取ResponseBody,method=get
   *
   * @param url exp:http://192.168.1.1:8080/dir/target.html
   * @return 以byte[]的方式放回
   */
  public static byte[] getDataFromUrl(String url, int timeout) {
    if (StringUtils.isBlank(url)) {
      logger.error("url is blank!");
      return null;
    }
    HttpClient httpClient = new HttpClient();
    // 连接超时
    httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(8000);
    // 等待数据返回超时
    httpClient.getParams().setSoTimeout(timeout);
    GetMethod method = new GetMethod(url);

    // fix连接结束后不能正确关闭的问题
    // method.setRequestHeader("Connection", "close");
    // 如果发生错误则连续尝试1次
    method
        .getParams()
        .setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(1, false));
    try {
      int statusCode = httpClient.executeMethod(method);
      if (statusCode == HttpStatus.SC_OK) {
        return method.getResponseBody();
      } else {
        throw new RuntimeException(
            "http request error,return code:"
                + statusCode
                + ",msg:"
                + new String(method.getResponseBody()));
      }
    } catch (HttpException e) {
      method.abort();
      logger.error(e.getMessage());
    } catch (IOException e) {
      method.abort();
      logger.error(e.getMessage());
    } finally {
      // Release the connection.
      method.releaseConnection();
    }
    return null;
  }