Java 类org.apache.commons.httpclient.HttpClient 实例源码

项目:neoscada    文件:HdHttpClient.java   
public List<DataPoint> getData ( final String item, final String type, final Date from, final Date to, final Integer number ) throws Exception
{
    final HttpClient client = new HttpClient ();
    final HttpMethod method = new GetMethod ( this.baseUrl + "/" + URLEncoder.encode ( item, "UTF-8" ) + "/" + URLEncoder.encode ( type, "UTF-8" ) + "?from=" + URLEncoder.encode ( Utils.isoDateFormat.format ( from ), "UTF-8" ) + "&to=" + URLEncoder.encode ( Utils.isoDateFormat.format ( to ), "UTF-8" ) + "&no=" + number );
    client.getParams ().setSoTimeout ( (int)this.timeout );
    try
    {
        final int status = client.executeMethod ( method );
        if ( status != HttpStatus.SC_OK )
        {
            throw new RuntimeException ( "Method failed with error " + status + " " + method.getStatusLine () );
        }
        return Utils.fromJson ( method.getResponseBodyAsString () );
    }
    finally
    {
        method.releaseConnection ();
    }
}
项目:logistimo-web-service    文件:LogiURLFetchService.java   
@Override
public HttpResponse get(URL urlObj, String userName, String password, int timeout) {
  HttpClient client = new HttpClient();
  HttpMethod method = new GetMethod(urlObj.toString());

  client.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
      new DefaultHttpMethodRetryHandler());
  client.getParams().setSoTimeout(1000 * timeout);
  client.getParams().setConnectionManagerTimeout(1000 * timeout);
  if (userName != null && password != null) {
    setBasicAuthorization(method, userName, password);
  }
  try {
    int response = client.executeMethod(method);
    return new HttpResponse(response, method.getResponseBody());
  } catch (IOException e) {
    throw new RuntimeException("Failed to get " + urlObj.toString(), e);
  } finally {
    method.releaseConnection();
  }
}
项目:Spring-Boot-Server    文件:HttpUtils.java   
public static String getPostResponseHeader(String url,String argJson,List<UHeader> headerList,String headerName){
    String info = "";
    try {
    HttpClient client = new HttpClient();
    PostMethod method = new PostMethod(url);
    client.getParams().setContentCharset("UTF-8");
    if(headerList.size()>0){
        for(int i = 0;i<headerList.size();i++){
            UHeader header = headerList.get(i);
            method.setRequestHeader(header.getHeaderTitle(),header.getHeaderValue());
        }
    }
    method.getParams().setParameter(
            HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
    if(argJson != null && !argJson.trim().equals("")) {
        RequestEntity requestEntity = new StringRequestEntity(argJson,"application/json","UTF-8");
        method.setRequestEntity(requestEntity);
    }
    method.releaseConnection();
    Header h =  method.getResponseHeader(headerName);
    info = h.getValue();
} catch (IOException e) {
    e.printStackTrace();
}
    return info;
  }
项目:alfresco-repository    文件:HttpClientTransmitterImpl.java   
public HttpClientTransmitterImpl()
{
    protocolMap = new TreeMap<String,Protocol>();
    protocolMap.put(HTTP_SCHEME_NAME, httpProtocol);
    protocolMap.put(HTTPS_SCHEME_NAME, httpsProtocol);

    httpClient = new HttpClient();
    httpClient.setHttpConnectionManager(new MultiThreadedHttpConnectionManager());
    httpMethodFactory = new StandardHttpMethodFactoryImpl();
    jsonErrorSerializer = new ExceptionJsonSerializer();

    // Create an HTTP Proxy Host if appropriate system properties are set
    httpProxyHost = HttpClientHelper.createProxyHost("http.proxyHost", "http.proxyPort", DEFAULT_HTTP_PORT);
    httpProxyCredentials = HttpClientHelper.createProxyCredentials("http.proxyUser", "http.proxyPassword");
    httpAuthScope = createProxyAuthScope(httpProxyHost);

    // Create an HTTPS Proxy Host if appropriate system properties are set
    httpsProxyHost = HttpClientHelper.createProxyHost("https.proxyHost", "https.proxyPort", DEFAULT_HTTPS_PORT);
    httpsProxyCredentials = HttpClientHelper.createProxyCredentials("https.proxyUser", "https.proxyPassword");
    httpsAuthScope = createProxyAuthScope(httpsProxyHost);
}
项目:alfresco-repository    文件:SolrQueryHTTPClient.java   
protected JSONResult postSolrQuery(HttpClient httpClient, String url, JSONObject body, SolrJsonProcessor<?> jsonProcessor, String spellCheckParams)
            throws UnsupportedEncodingException, IOException, HttpException, URIException,
            JSONException
{
    JSONObject json = postQuery(httpClient, url, body);
    if (spellCheckParams != null)
    {
        SpellCheckDecisionManager manager = new SpellCheckDecisionManager(json, url, body, spellCheckParams);
        if (manager.isCollate())
        {
            json = postQuery(httpClient, manager.getUrl(), body);
        }
        json.put("spellcheck", manager.getSpellCheckJsonValue());
    }

        JSONResult results = jsonProcessor.getResult(json);

        if (s_logger.isDebugEnabled())
        {
            s_logger.debug("Sent :" + url);
            s_logger.debug("   with: " + body.toString());
            s_logger.debug("Got: " + results.getNumberFound() + " in " + results.getQueryTime() + " ms");
        }

        return results;
}
项目:alfresco-repository    文件:ExplicitSolrStoreMappingWrapper.java   
/**
 * @return
 */
public Pair<HttpClient, String> getHttpClientAndBaseUrl()
{

    if (!policy.configurationIsValid())
    {
        throw new AlfrescoRuntimeException("Invalid shard configuration: shard = "
                + wrapped.getNumShards() + "   reoplicationFactor = " + wrapped.getReplicationFactor() + " with node count = " + httpClientsAndBaseURLs.size());
    }

    int shard = random.nextInt(wrapped.getNumShards());
    int position =  random.nextInt(wrapped.getReplicationFactor());
    List<Integer> nodeInstances = policy.getNodeInstancesForShardId(shard);
    Integer nodeId = nodeInstances.get(position);         
    HttpClientAndBaseUrl httpClientAndBaseUrl = httpClientsAndBaseURLs.toArray(new HttpClientAndBaseUrl[0])[nodeId-1];
    return new Pair<>(httpClientAndBaseUrl.httpClient, isSharded() ? httpClientAndBaseUrl.baseUrl+"-"+shard : httpClientAndBaseUrl.baseUrl);
}
项目:automat    文件:HttpUtil.java   
public static final String httpClientPost(String url, ArrayList<NameValuePair> list) {
    String result = "";
    HttpClient client = new HttpClient();
    PostMethod postMethod = new PostMethod(url);
    try {
        NameValuePair[] params = new NameValuePair[list.size()];
        for (int i = 0; i < list.size(); i++) {
            params[i] = list.get(i);
        }
        postMethod.addParameters(params);
        client.executeMethod(postMethod);
        result = postMethod.getResponseBodyAsString();
    } catch (Exception e) {
        logger.error(e);
    } finally {
        postMethod.releaseConnection();
    }
    return result;
}
项目:alfresco-core    文件:HttpClientFactory.java   
protected HttpClient constructHttpClient()
{
    MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
    HttpClient httpClient = new HttpClient(connectionManager);
    HttpClientParams params = httpClient.getParams();
    params.setBooleanParameter(HttpConnectionParams.TCP_NODELAY, true);
    params.setBooleanParameter(HttpConnectionParams.STALE_CONNECTION_CHECK, true);
    if (socketTimeout != null) 
    {
        params.setSoTimeout(socketTimeout);
    }
    HttpConnectionManagerParams connectionManagerParams = httpClient.getHttpConnectionManager().getParams();
    connectionManagerParams.setMaxTotalConnections(maxTotalConnections);
    connectionManagerParams.setDefaultMaxConnectionsPerHost(maxHostConnections);
    connectionManagerParams.setConnectionTimeout(connectionTimeout);

    return httpClient;
}
项目:alfresco-core    文件:HttpClientFactory.java   
public HttpClient getHttpClient()
{
    HttpClient httpClient = null;

    if(secureCommsType == SecureCommsType.HTTPS)
    {
        httpClient = getHttpsClient();
    }
    else if(secureCommsType == SecureCommsType.NONE)
    {
        httpClient = getDefaultHttpClient();
    }
    else
    {
        throw new AlfrescoRuntimeException("Invalid Solr secure communications type configured in alfresco.secureComms, should be 'ssl'or 'none'");
    }

    return httpClient;
}
项目:alfresco-core    文件:HttpClientFactory.java   
public HttpClient getHttpClient(String host, int port)
{
    HttpClient httpClient = null;

    if(secureCommsType == SecureCommsType.HTTPS)
    {
        httpClient = getHttpsClient(host, port);
    }
    else if(secureCommsType == SecureCommsType.NONE)
    {
        httpClient = getDefaultHttpClient(host, port);
    }
    else
    {
        throw new AlfrescoRuntimeException("Invalid Solr secure communications type configured in alfresco.secureComms, should be 'ssl'or 'none'");
    }

    return httpClient;
}
项目:jrpip    文件:FastServletProxyFactory.java   
/**
 * @return the http response code returned from the server. Response code 200 means success.
 */
public static int fastFailPing(AuthenticatedUrl url) throws IOException
{
    PingRequest pingRequest = null;
    try
    {
        HttpClient httpClient = getHttpClient(url);
        pingRequest = new PingRequest(url.getPath());
        HttpMethodParams params = pingRequest.getParams();
        params.setSoTimeout(PING_TIMEOUT);
        httpClient.executeMethod(pingRequest);
        return pingRequest.getStatusCode();
    }
    finally
    {
        if (pingRequest != null)
        {
            pingRequest.releaseConnection();
        }
    }
}
项目:jrpip    文件:FastServletProxyFactory.java   
public static HttpClient getHttpClient(AuthenticatedUrl url)
{
    HttpClient httpClient = new HttpClient(HTTP_CONNECTION_MANAGER);
    httpClient.getHostConfiguration().setHost(url.getHost(), url.getPort());
    httpClient.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, NoRetryRetryHandler.getInstance());
    url.setCredentialsOnClient(httpClient);
    return httpClient;
}
项目:jrpip    文件:FastServletProxyFactory.java   
public static Cookie[] requestNewSession(AuthenticatedUrl url)
{
    CreateSessionRequest createSessionRequest = null;
    try
    {
        HttpClient httpClient = getHttpClient(url);
        createSessionRequest = new CreateSessionRequest(url.getPath());
        HttpMethodParams params = createSessionRequest.getParams();
        params.setSoTimeout(PING_TIMEOUT);
        httpClient.executeMethod(createSessionRequest);
        return httpClient.getState().getCookies();
    }
    catch (Exception e)
    {
        throw new RuntimeException("Failed to create session", e);
    }
    finally
    {
        if (createSessionRequest != null)
        {
            createSessionRequest.releaseConnection();
        }
    }
}
项目:logistimo-web-service    文件:LogiURLFetchService.java   
@Override
public HttpResponse post(URL urlObj, byte[] payload, String userName, String password,
                         int timeout) {
  HttpClient client = new HttpClient();
  PostMethod method = new PostMethod(urlObj.toString());
  method.setRequestEntity(new ByteArrayRequestEntity(payload));
  method.setRequestHeader("Content-type", "application/json");
  client.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
      new DefaultHttpMethodRetryHandler());
  client.getParams().setSoTimeout(1000 * timeout);
  client.getParams().setConnectionManagerTimeout(1000 * timeout);
  if (userName != null && password != null) {
    setBasicAuthorization(method, userName, password);
  }
  try {
    int response = client.executeMethod(method);
    return new HttpResponse(response, method.getResponseBody());
  } catch (IOException e) {
    throw new RuntimeException("Failed to process post request URL: " + urlObj, e);
  } finally {
    method.releaseConnection();
  }

}
项目:convertigo-engine    文件:PacManager.java   
private String downloadPacContent(String url) throws IOException {
    if (url == null) {
        Engine.logProxyManager.debug("(PacManager) Invalid PAC script URL: null");
        throw new IOException("Invalid PAC script URL: null");
    }

    HttpClient client = new HttpClient();
    HttpMethod method = new GetMethod(url);

    int statusCode = client.executeMethod(method);

    if (statusCode != HttpStatus.SC_OK) {
        throw new IOException("(PacManager) Method failed: " + method.getStatusLine());
    }

    return IOUtils.toString(method.getResponseBodyAsStream(), "UTF-8");
}
项目:uavstack    文件:HttpService.java   
/**
 * 同步客户端3.X版本
 * 
 * @return
 */
@GET
@Path("httpclient3test")
public String httpClient3Test() {

    HttpClient httpClient = new HttpClient();
    HttpMethod method = new GetMethod("https://www.baidu.com/");

    try {
        httpClient.executeMethod(method);
        System.out.println(method.getURI());
        System.out.println(method.getStatusLine());
        System.out.println(method.getName());
        System.out.println(method.getResponseHeader("Server").getValue());
        System.out.println(method.getResponseBodyAsString());
    }
    catch (Exception e) {
        e.printStackTrace();
    }

    return "httpClient3 test success";
}
项目:convertigo-engine    文件:WsReference.java   
private static void tryAuthentication(RemoteFileReference wsReference) throws Exception {
    URL urlToConnect = wsReference.getUrl();
    String wsdlUrl = wsReference.getUrlpath();
    String username = wsReference.getAuthUser();
    String password = wsReference.getAuthPassword();

       HttpClient client = new HttpClient();

    client.getState().setCredentials(
            new AuthScope(urlToConnect.getHost(), urlToConnect.getPort()),
            new UsernamePasswordCredentials(username, password)
    );

       GetMethod get = new GetMethod(wsdlUrl);
       get.setDoAuthentication( true );

       int statuscode = client.executeMethod(get);

       if (statuscode == HttpStatus.SC_UNAUTHORIZED) {
        throw new Exception(HttpStatus.SC_UNAUTHORIZED + " - Unauthorized connection!");
       }
}
项目:lams    文件:HTTPMetadataProvider.java   
/**
 * Constructor.
 * 
 * @param metadataURL the URL to fetch the metadata
 * @param requestTimeout the time, in milliseconds, to wait for the metadata server to respond
 * 
 * @throws MetadataProviderException thrown if the URL is not a valid URL or the metadata can not be retrieved from
 *             the URL
 */
@Deprecated
public HTTPMetadataProvider(String metadataURL, int requestTimeout) throws MetadataProviderException {
    super();
    try {
        metadataURI = new URI(metadataURL);
    } catch (URISyntaxException e) {
        throw new MetadataProviderException("Illegal URL syntax", e);
    }

    HttpClientParams clientParams = new HttpClientParams();
    clientParams.setSoTimeout(requestTimeout);
    httpClient = new HttpClient(clientParams);
    httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(requestTimeout);
    authScope = new AuthScope(metadataURI.getHost(), metadataURI.getPort());

}
项目:lams    文件:HTTPMetadataProvider.java   
/**
 * Constructor.
 * 
 * @param client HTTP client used to pull in remote metadata
 * @param backgroundTaskTimer timer used to schedule background metadata refresh tasks
 * @param metadataURL URL to the remove remote metadata
 * 
 * @throws MetadataProviderException thrown if the HTTP client is null or the metadata URL provided is invalid
 */
public HTTPMetadataProvider(Timer backgroundTaskTimer, HttpClient client, String metadataURL)
        throws MetadataProviderException {
    super(backgroundTaskTimer);

    if (client == null) {
        throw new MetadataProviderException("HTTP client may not be null");
    }
    httpClient = client;

    try {
        metadataURI = new URI(metadataURL);
    } catch (URISyntaxException e) {
        throw new MetadataProviderException("Illegal URL syntax", e);
    }

    authScope = new AuthScope(metadataURI.getHost(), metadataURI.getPort());
}
项目:iBase4J    文件:HttpUtil.java   
public static final String httpClientPost(String url, ArrayList<NameValuePair> list) {
    String result = "";
    HttpClient client = new HttpClient();
    PostMethod postMethod = new PostMethod(url);
    try {
        NameValuePair[] params = new NameValuePair[list.size()];
        for (int i = 0; i < list.size(); i++) {
            params[i] = list.get(i);
        }
        postMethod.addParameters(params);
        client.executeMethod(postMethod);
        result = postMethod.getResponseBodyAsString();
    } catch (Exception e) {
        logger.error(e);
    } finally {
        postMethod.releaseConnection();
    }
    return result;
}
项目:JAVA-    文件:HttpUtil.java   
public static final String httpClientPost(String url, ArrayList<NameValuePair> list) {
    String result = "";
    HttpClient client = new HttpClient();
    PostMethod postMethod = new PostMethod(url);
    try {
        NameValuePair[] params = new NameValuePair[list.size()];
        for (int i = 0; i < list.size(); i++) {
            params[i] = list.get(i);
        }
        postMethod.addParameters(params);
        client.executeMethod(postMethod);
        result = postMethod.getResponseBodyAsString();
    } catch (Exception e) {
        logger.error(e);
    } finally {
        postMethod.releaseConnection();
    }
    return result;
}
项目:Hydrograph    文件:HydrographServiceClient.java   
public void chcekConnectionStatus() throws IOException {

        HttpClient httpClient = new HttpClient();
        //TODO : add connection details while testing only,remove it once done
        String teradatajson = "{\"username\":\"\",\"password\":\"\",\"hostname\":\"\",\"database\":\"\",\"dbtype\":\"\",\"port\":\"\"}";
        PostMethod postMethod = new PostMethod("http://" + HOST_NAME + ":" + PORT + "/getConnectionStatus");

        //postMethod.addParameter("request_parameters", redshiftjson);
        postMethod.addParameter("request_parameters", teradatajson);

        int response = httpClient.executeMethod(postMethod);
        InputStream inputStream = postMethod.getResponseBodyAsStream();

        byte[] buffer = new byte[1024 * 1024 * 5];
        String path = null;
        int length;
        while ((length = inputStream.read(buffer)) > 0) {
            path = new String(buffer);
        }
        System.out.println("Response of service: " + path);
        System.out.println("==================");
    }
项目:Hydrograph    文件:HydrographServiceClient.java   
public void calltoReadMetastore() throws IOException {

        HttpClient httpClient = new HttpClient();
      //TODO : add connection details while testing only,remove it once done
        String teradatajson = "{\"table\":\"testting2\",\"username\":\"\",\"password\":\"\",\"hostname\":\"\",\"database\":\"\",\"dbtype\":\"\",\"port\":\"\"}";

        PostMethod postMethod = new PostMethod("http://" + HOST_NAME + ":" + PORT + "/readFromMetastore");

        //postMethod.addParameter("request_parameters", redshiftjson);
        postMethod.addParameter("request_parameters", teradatajson);

        int response = httpClient.executeMethod(postMethod);
        InputStream inputStream = postMethod.getResponseBodyAsStream();

        byte[] buffer = new byte[1024 * 1024 * 5];
        String path = null;
        int length;
        while ((length = inputStream.read(buffer)) > 0) {
            path = new String(buffer);
        }
        System.out.println("Response of service: " + path);
        System.out.println("==================");
    }
项目:hadoop    文件:SwiftRestClient.java   
/**
 * Execute the request with the request and response logged at debug level
 * @param method method to execute
 * @param client client to use
 * @param <M> method type
 * @return the status code
 * @throws IOException any failure reported by the HTTP client.
 */
private <M extends HttpMethod> int execWithDebugOutput(M method,
                                                       HttpClient client) throws
        IOException {
  if (LOG.isDebugEnabled()) {
    StringBuilder builder = new StringBuilder(
            method.getName() + " " + method.getURI() + "\n");
    for (Header header : method.getRequestHeaders()) {
      builder.append(header.toString());
    }
    LOG.debug(builder);
  }
  int statusCode = client.executeMethod(method);
  if (LOG.isDebugEnabled()) {
    LOG.debug("Status code = " + statusCode);
  }
  return statusCode;
}
项目:lib-commons-httpclient    文件:MultiThreadedExample.java   
public static void main(String[] args) {

    // Create an HttpClient with the MultiThreadedHttpConnectionManager.
    // This connection manager must be used if more than one thread will
    // be using the HttpClient.
    HttpClient httpClient = new HttpClient(new MultiThreadedHttpConnectionManager());
    // Set the default host/protocol for the methods to connect to.
    // This value will only be used if the methods are not given an absolute URI
    httpClient.getHostConfiguration().setHost("jakarta.apache.org", 80, "http");

    // create an array of URIs to perform GETs on
    String[] urisToGet = {
        "/",
        "/commons/",
        "/commons/httpclient/",
        "http://svn.apache.org/viewvc/jakarta/httpcomponents/oac.hc3x/"
    };

    // create a thread for each URI
    GetThread[] threads = new GetThread[urisToGet.length];
    for (int i = 0; i < threads.length; i++) {
        GetMethod get = new GetMethod(urisToGet[i]);
        get.setFollowRedirects(true);
        threads[i] = new GetThread(httpClient, get, i + 1);
    }

    // start the threads
    for (int j = 0; j < threads.length; j++) {
        threads[j].start();
    }

}
项目:iBase4J-Common    文件:HttpUtil.java   
public static final String httpClientPost(String url) {
    String result = "";
    HttpClient client = new HttpClient();
    GetMethod getMethod = new GetMethod(url);
    try {
        client.executeMethod(getMethod);
        result = getMethod.getResponseBodyAsString();
    } catch (Exception e) {
        logger.error("", e);
    } finally {
        getMethod.releaseConnection();
    }
    return result;
}
项目:ditb    文件:Client.java   
private void initialize(Cluster cluster, boolean sslEnabled) {
  this.cluster = cluster;
  this.sslEnabled = sslEnabled;
  MultiThreadedHttpConnectionManager manager =
    new MultiThreadedHttpConnectionManager();
  HttpConnectionManagerParams managerParams = manager.getParams();
  managerParams.setConnectionTimeout(2000); // 2 s
  managerParams.setDefaultMaxConnectionsPerHost(10);
  managerParams.setMaxTotalConnections(100);
  extraHeaders = new ConcurrentHashMap<String, String>();
  this.httpClient = new HttpClient(manager);
  HttpClientParams clientParams = httpClient.getParams();
  clientParams.setVersion(HttpVersion.HTTP_1_1);

}
项目:DBus    文件:HttpRequest.java   
public String doPost(String url, String charset, String jsonObj) {
    String resStr = null;
    HttpClient htpClient = new HttpClient();
    PostMethod postMethod = new PostMethod(url);
    postMethod.getParams().setParameter(
            HttpMethodParams.HTTP_CONTENT_CHARSET, charset);
    try {
        postMethod.setRequestEntity(new StringRequestEntity(jsonObj,
                "application/json", charset));
        int statusCode = htpClient.executeMethod(postMethod);
        if (statusCode != HttpStatus.SC_OK) {
            // post和put不能自动处理转发 301:永久重定向,告诉客户端以后应从新地址访问 302:Moved
            if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY
                    || statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {
                Header locationHeader = postMethod
                        .getResponseHeader("location");
                String location = null;
                if (locationHeader != null) {
                    location = locationHeader.getValue();
                    log.info("The page was redirected to :" + location);
                } else {
                    log.info("Location field value is null");
                }
            } else {
                log.error("Method failed: " + postMethod.getStatusLine());
            }
            return resStr;
        }
        byte[] responseBody = postMethod.getResponseBody();
        resStr = new String(responseBody, charset);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        postMethod.releaseConnection();
    }
    return resStr;
}
项目:lib-commons-httpclient    文件:HttpBenchmark.java   
private static HttpClient createRequestExecutor() {
    HttpClient httpclient = new HttpClient();
    httpclient.getParams().setVersion(HttpVersion.HTTP_1_1);
    httpclient.getParams().setBooleanParameter(HttpMethodParams.USE_EXPECT_CONTINUE, false);
    httpclient.getHttpConnectionManager().getParams().setStaleCheckingEnabled(false);
    return httpclient;
}
项目:geomapapp    文件:Tile512Overlay.java   
private static BufferedImage readHTTPImage(URI uri,
        HostConfiguration hostConfiguration, HttpClient client,
        ConnectionWrapper wrapper) {
    List<Proxy> list = ProxySelector.getDefault().select(uri);
    for (Proxy p : list)
    {
        InetSocketAddress addr = (InetSocketAddress) p.address();

        if (addr == null)
            hostConfiguration.setProxyHost(null);
        else
            hostConfiguration.setProxy(addr.getHostName(), addr.getPort());

        try {
            HttpMethod method = new GetMethod(uri.toString());
            synchronized (wrapper) {
                wrapper.connection = method;
            }

            int sc = client.executeMethod(hostConfiguration, method);

            if (sc != HttpStatus.SC_OK) {
                continue;
            }

            // Check Content Type
            Header h = method.getResponseHeader("Content-Type");
            if (h == null || !h.getValue().contains("image")) 
                continue;

            return ImageIO.read( method.getResponseBodyAsStream() );
        } catch (IOException ex) {
            continue;
        }
    }
    return null;
}
项目:alfresco-repository    文件:DynamicSolrStoreMappingWrapperFactory.java   
@Override
public Pair<HttpClient, String> getHttpClientAndBaseUrl()
{
   int base = ThreadLocalRandom.current().nextInt(slice.size());
   ShardInstance instance = slice.get(base);
   Pair<String, Integer> key = new Pair<String, Integer>(instance.getHostName(), instance.getPort());
   HttpClient client = clients.get(key);
   return new Pair<HttpClient, String>(client, instance.getBaseUrl());
}
项目:lib-commons-httpclient    文件:ChunkEncodedPost.java   
public static void main(String[] args) throws Exception {
  if (args.length != 1)  {
      System.out.println("Usage: ChunkEncodedPost <file>");
      System.out.println("<file> - full path to a file to be posted");
      System.exit(1);
  }
  HttpClient client = new HttpClient();

  PostMethod httppost = new PostMethod("http://localhost:8080/httpclienttest/body");

  File file = new File(args[0]);

  httppost.setRequestEntity(new InputStreamRequestEntity(new FileInputStream(file)));
  httppost.setContentChunked(true);

  try {
      client.executeMethod(httppost);

      if (httppost.getStatusCode() == HttpStatus.SC_OK) {
          System.out.println(httppost.getResponseBodyAsString());
      } else {
        System.out.println("Unexpected failure: " + httppost.getStatusLine().toString());
      }
  } finally {
      httppost.releaseConnection();
  }   
}
项目:scim2-compliance-test-suite    文件:CSP.java   
public String getAccessTokenUserPass() {
    if (!StringUtils.isEmpty(this.oAuth2AccessToken)) {
        return this.oAuth2AccessToken;
    }

    if (StringUtils.isEmpty(this.username) || StringUtils.isEmpty(this.password) && StringUtils.isEmpty(this.oAuth2AuthorizationServer)
            || StringUtils.isEmpty(this.oAuth2ClientId) || StringUtils.isEmpty(this.oAuth2ClientSecret)) {
        return "";
    }

    try {
        HttpClient client = new HttpClient();
        client.getParams().setAuthenticationPreemptive(true);

        // post development
        PostMethod method = new PostMethod(this.getOAuthAuthorizationServer());
        method.setRequestHeader(new Header("Content-type", "application/x-www-form-urlencoded"));

        method.addRequestHeader("Authorization", "Basic " + Base64.encodeBase64String((username + ":" + password).getBytes()));
        NameValuePair[] body = new NameValuePair[] { new NameValuePair("username", username), new NameValuePair("password", password),
                new NameValuePair("client_id", oAuth2ClientId), new NameValuePair("client_secret", oAuth2ClientSecret),
                new NameValuePair("grant_type", oAuth2GrantType) };
        method.setRequestBody(body);
        int responseCode = client.executeMethod(method);

        String responseBody = method.getResponseBodyAsString();
        if (responseCode != 200) {
            throw new RuntimeException("Failed to fetch access token form authorization server, " + this.getOAuthAuthorizationServer()
                    + ", got response code " + responseCode);
        }

        JSONObject accessResponse = new JSONObject(responseBody);
        accessResponse.getString("access_token");
        return (this.oAuth2AccessToken = accessResponse.getString("access_token"));
    } catch (Exception e) {
        throw new RuntimeException("Failed to read response from authorizationServer at " + this.getOAuthAuthorizationServer(), e);
    }
}
项目:alfresco-repository    文件:BaseWebScriptTest.java   
@Override
protected void setUp() throws Exception
{
    super.setUp();

    if (remoteServer != null)
    {
        httpClient = new HttpClient();
        httpClient.getParams().setBooleanParameter(HttpClientParams.PREEMPTIVE_AUTHENTICATION, true);
        if (remoteServer.username != null)
        {
            httpClient.getState().setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), new UsernamePasswordCredentials(remoteServer.username, remoteServer.password));
        }
    }
}
项目:alfresco-repository    文件:SolrStoreMappingWrapperTest.java   
@Test
public void testUnsharded()
{
    assertTrue(unshardedWrapper.isSharded() == false);

    Pair<HttpClient, String> distributor = unshardedWrapper.getHttpClientAndBaseUrl();
    assertNotNull(distributor);
    assertEquals("/solr4", distributor.getSecond());
    assertEquals("common", distributor.getFirst().getHostConfiguration().getHost());
    assertEquals(999, distributor.getFirst().getHostConfiguration().getPort());
}
项目:automat    文件:HttpUtil.java   
public static final String httpClientPost(String url) {
    String result = "";
    HttpClient client = new HttpClient();
    GetMethod getMethod = new GetMethod(url);
    try {
        client.executeMethod(getMethod);
        result = getMethod.getResponseBodyAsString();
    } catch (Exception e) {
        logger.error(e);
    } finally {
        getMethod.releaseConnection();
    }
    return result;
}
项目:Babler    文件:BabelProducer.java   
public BabelProducer(BabelBroker broker, HttpClient httpClient, String language) {
    this.broker = broker;
    this.lang = language;
    this.httpClient = httpClient;
    this.words = LanguageDataManager.getMostCommonWords(this.lang, 3000, ngram);
    this.logDb =  new LogDB(this.lang);
    this.usersLogDB = new LogDB(this.lang,"TopsyUsers");

}
项目:Babler    文件:BabelProducer.java   
public BabelProducer(BabelBroker broker, HttpClient httpClient, String language, boolean user) {
    this.broker = broker;
    this.lang = language;
    this.httpClient = httpClient;
    this.logDb =  new LogDB(this.lang);
    this.usersLogDB = new LogDB(this.lang,"TopsyUsers");
    this.byUsers = user;

    if(byUsers)
        //this.users = getUserIDsFromFile();
        this.users = getUsersFromDB();
    else
        this.words = LanguageDataManager.getMostCommonWords(this.lang, 3000, ngram);

}
项目:alfresco-core    文件:HttpClientFactory.java   
protected HttpClient getHttpsClient(String httpsHost, int httpsPort)
{
    // Configure a custom SSL socket factory that will enforce mutual authentication
    HttpClient httpClient = constructHttpClient();
    HttpHostFactory hostFactory = new HttpHostFactory(new Protocol("https", sslSocketFactory, httpsPort));
    httpClient.setHostConfiguration(new HostConfigurationWithHostFactory(hostFactory));
    httpClient.getHostConfiguration().setHost(httpsHost, httpsPort, "https");
    return httpClient;
}
项目:jrpip    文件:AuthenticatedUrl.java   
public void setCredentialsOnClient(HttpClient client)
{
    if (this.credentials != null)
    {
        client.getParams().setAuthenticationPreemptive(true);
        client.getState().setCredentials(AuthScope.ANY, this.credentials);
    }

    if (this.cookies != null && this.cookies.length > 0)
    {
        client.getState().addCookies(this.cookies);
    }
}