当前位置: 首页>>代码示例>>Java>>正文


Java HTTP类代码示例

本文整理汇总了Java中org.apache.http.protocol.HTTP的典型用法代码示例。如果您正苦于以下问题:Java HTTP类的具体用法?Java HTTP怎么用?Java HTTP使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


HTTP类属于org.apache.http.protocol包,在下文中一共展示了HTTP类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: getNewHttpClient

import org.apache.http.protocol.HTTP; //导入依赖的package包/类
private static HttpClient getNewHttpClient() { 
   try { 
       KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); 
       trustStore.load(null, null); 

       SSLSocketFactory sf = new SSLSocketFactoryEx(trustStore); 
       sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); 

       HttpParams params = new BasicHttpParams(); 
       HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); 
       HttpProtocolParams.setContentCharset(params, HTTP.UTF_8); 

       SchemeRegistry registry = new SchemeRegistry(); 
       registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); 
       registry.register(new Scheme("https", sf, 443)); 

       ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry); 

       return new DefaultHttpClient(ccm, params); 
   } catch (Exception e) { 
       return new DefaultHttpClient(); 
   } 
}
 
开发者ID:gyqGitHub,项目名称:WeiXinPayDemo,代码行数:24,代码来源:Util.java

示例2: getFileNameFromHttpResponse

import org.apache.http.protocol.HTTP; //导入依赖的package包/类
public static String getFileNameFromHttpResponse(final HttpResponse response) {
    if (response == null) return null;
    String result = null;
    Header header = response.getFirstHeader("Content-Disposition");
    if (header != null) {
        for (HeaderElement element : header.getElements()) {
            NameValuePair fileNamePair = element.getParameterByName("filename");
            if (fileNamePair != null) {
                result = fileNamePair.getValue();
                // try to get correct encoding str
                result = CharsetUtils.toCharset(result, HTTP.UTF_8, result.length());
                break;
            }
        }
    }
    return result;
}
 
开发者ID:SavorGit,项目名称:Hotspot-master-devp,代码行数:18,代码来源:OtherUtils.java

示例3: getNewHttpClient

import org.apache.http.protocol.HTTP; //导入依赖的package包/类
private static HttpClient getNewHttpClient() { 
   try { 
       KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
       trustStore.load(null, null); 

       SSLSocketFactory sf = new SSLSocketFactoryEx(trustStore);
       sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

       HttpParams params = new BasicHttpParams();
       HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); 
       HttpProtocolParams.setContentCharset(params, HTTP.UTF_8); 

       SchemeRegistry registry = new SchemeRegistry(); 
       registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); 
       registry.register(new Scheme("https", sf, 443)); 

       ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry); 

       return new DefaultHttpClient(ccm, params); 
   } catch (Exception e) {
       return new DefaultHttpClient(); 
   } 
}
 
开发者ID:cheng2016,项目名称:developNote,代码行数:24,代码来源:HttpUtil.java

示例4: parseCharset

import org.apache.http.protocol.HTTP; //导入依赖的package包/类
/**
 * Retrieve a charset from headers
 *
 * @param headers An {@link java.util.Map} of headers
 * @param defaultCharset Charset to return if none can be found
 * @return Returns the charset specified in the Content-Type of this header,
 * or the defaultCharset if none can be found.
 */
public static String parseCharset(Map<String, String> headers, String defaultCharset) {
    String contentType = headers.get(HTTP.CONTENT_TYPE);
    if (contentType != null) {
        String[] params = contentType.split(";");
        for (int i = 1; i < params.length; i++) {
            String[] pair = params[i].trim().split("=");
            if (pair.length == 2) {
                if (pair[0].equals("charset")) {
                    return pair[1];
                }
            }
        }
    }

    return defaultCharset;
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:25,代码来源:HttpHeaderParser.java

示例5: parseCharset

import org.apache.http.protocol.HTTP; //导入依赖的package包/类
/**
 * Retrieve a charset from headers
 *
 * @param headers An {@link Map} of headers
 * @param defaultCharset Charset to return if none can be found
 * @return Returns the charset specified in the Content-Type of this header,
 * or the defaultCharset if none can be found.
 */
public static String parseCharset(Map<String, String> headers, String defaultCharset) {
    String contentType = headers.get(HTTP.CONTENT_TYPE);
    if (contentType != null) {
        String[] params = contentType.split(";");
        for (int i = 1; i < params.length; i++) {
            String[] pair = params[i].trim().split("=");
            if (pair.length == 2) {
                if (pair[0].equals("charset")) {
                    return pair[1];
                }
            }
        }
    }

    return defaultCharset;
}
 
开发者ID:wangzhaosheng,项目名称:publicProject,代码行数:25,代码来源:HttpHeaderParser.java

示例6: createHttpClient

import org.apache.http.protocol.HTTP; //导入依赖的package包/类
/**
 * 设置默认请求参数,并返回HttpClient
 *
 * @return HttpClient
 */
private HttpClient createHttpClient() {
    HttpParams mDefaultHttpParams = new BasicHttpParams();
    //设置连接超时
    HttpConnectionParams.setConnectionTimeout(mDefaultHttpParams, 15000);
    //设置请求超时
    HttpConnectionParams.setSoTimeout(mDefaultHttpParams, 15000);
    HttpConnectionParams.setTcpNoDelay(mDefaultHttpParams, true);
    HttpProtocolParams.setVersion(mDefaultHttpParams, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(mDefaultHttpParams, HTTP.UTF_8);
    //持续握手
    HttpProtocolParams.setUseExpectContinue(mDefaultHttpParams, true);
    HttpClient mHttpClient = new DefaultHttpClient(mDefaultHttpParams);
    return mHttpClient;

}
 
开发者ID:henrymorgen,项目名称:android-advanced-light,代码行数:21,代码来源:MainActivity.java

示例7: buildEntity

import org.apache.http.protocol.HTTP; //导入依赖的package包/类
/**
 * Build the HttpEntity to be sent to the Service as part of (POST) request. Creates a off-memory
 * {@link FileExposingFileEntity} or a regular in-memory {@link ByteArrayEntity} depending on if the request
 * OutputStream fit into memory when built by calling.
 *
 * @param request -
 * @return - the built HttpEntity
 * @throws IOException -
 */
protected HttpEntity buildEntity(final ClientInvocation request) throws IOException {
  HttpEntity entityToBuild = null;
  DeferredFileOutputStream memoryManagedOutStream = writeRequestBodyToOutputStream(request);

  if (memoryManagedOutStream.isInMemory()) {
    ByteArrayEntity entityToBuildByteArray =
        new ByteArrayEntity(memoryManagedOutStream.getData());
    entityToBuildByteArray.setContentType(
        new BasicHeader(HTTP.CONTENT_TYPE, request.getHeaders().getMediaType().toString()));
    entityToBuild = entityToBuildByteArray;
  } else {
    File requestBodyFile = memoryManagedOutStream.getFile();
    requestBodyFile.deleteOnExit();
    entityToBuild = new FileExposingFileEntity(
        memoryManagedOutStream.getFile(), request.getHeaders().getMediaType().toString());
  }

  return entityToBuild;
}
 
开发者ID:cerner,项目名称:beadledom,代码行数:29,代码来源:ApacheHttpClient4Dot3Engine.java

示例8: getNewHttpClient

import org.apache.http.protocol.HTTP; //导入依赖的package包/类
/**
 * Gets getUrl DefaultHttpClient which trusts getUrl set of certificates specified by the KeyStore
 *
 * @param keyStore custom provided KeyStore instance
 * @return DefaultHttpClient
 */
public static DefaultHttpClient getNewHttpClient(KeyStore keyStore) {

    try {
        SSLSocketFactory sf = new MySSLSocketFactory(keyStore);
        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        registry.register(new Scheme("https", sf, 443));

        HttpParams params = new BasicHttpParams();
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);

        ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);

        return new DefaultHttpClient(ccm, params);
    } catch (Exception e) {
        return new DefaultHttpClient();
    }
}
 
开发者ID:yiwent,项目名称:Mobike,代码行数:26,代码来源:MySSLSocketFactory.java

示例9: post

import org.apache.http.protocol.HTTP; //导入依赖的package包/类
@Deprecated
public static String post(String Url, List<NameValuePair> params) {
    String strResult = null;
    HttpResponse httpResponse;
    HttpPost httpRequest = new HttpPost(Url);
    try {
        httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
        httpResponse = new DefaultHttpClient().execute(httpRequest);
        if (httpResponse.getStatusLine().getStatusCode() == 200) {
            strResult = EntityUtils.toString(httpResponse.getEntity());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return strResult;
}
 
开发者ID:quickhybrid,项目名称:quickhybrid-android,代码行数:17,代码来源:HttpUtil.java

示例10: send

import org.apache.http.protocol.HTTP; //导入依赖的package包/类
@Override
public void send(CrashReportData report) throws ReportSenderException {
  String log = createCrashLog(report);
  String url = BASE_URL + ACRA.getConfig().formKey() + CRASHES_PATH;

  try {
    DefaultHttpClient httpClient = new DefaultHttpClient(); 
    HttpPost httpPost = new HttpPost(url);

    List<NameValuePair> parameters = new ArrayList<NameValuePair>(); 
    parameters.add(new BasicNameValuePair("raw", log));
    parameters.add(new BasicNameValuePair("userID", report.get(ReportField.INSTALLATION_ID)));
    parameters.add(new BasicNameValuePair("contact", report.get(ReportField.USER_EMAIL)));
    parameters.add(new BasicNameValuePair("description", report.get(ReportField.USER_COMMENT)));
    
    httpPost.setEntity(new UrlEncodedFormEntity(parameters, HTTP.UTF_8));

    httpClient.execute(httpPost);   
  }
  catch (Exception e) {
    e.printStackTrace();
  } 
}
 
开发者ID:WorldBank-Transport,项目名称:RoadLab-Pro,代码行数:24,代码来源:HockeySender.java

示例11: MultipartEntity

import org.apache.http.protocol.HTTP; //导入依赖的package包/类
/**
 * Creates an instance using the specified parameters
 *
 * @param mode     the mode to use, may be {@code null}, in which case {@link HttpMultipartMode#STRICT} is used
 * @param boundary the boundary string, may be {@code null}, in which case {@link #generateBoundary()} is invoked to create the string
 * @param charset  the character set to use, may be {@code null}, in which case {@link MIME#DEFAULT_CHARSET} - i.e. UTF-8 - is used.
 */
public MultipartEntity(
        HttpMultipartMode mode,
        String boundary,
        Charset charset) {
    super();
    if (boundary == null) {
        boundary = generateBoundary();
    }
    this.boundary = boundary;
    if (mode == null) {
        mode = HttpMultipartMode.STRICT;
    }
    this.charset = charset != null ? charset : MIME.DEFAULT_CHARSET;
    this.multipart = new HttpMultipart(multipartSubtype, this.charset, this.boundary, mode);
    this.contentType = new BasicHeader(
            HTTP.CONTENT_TYPE,
            generateContentType(this.boundary, this.charset));
    this.dirty = true;
}
 
开发者ID:SavorGit,项目名称:Hotspot-master-devp,代码行数:27,代码来源:MultipartEntity.java

示例12: getKeepAliveDuration

import org.apache.http.protocol.HTTP; //导入依赖的package包/类
@Override
public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
  HeaderElementIterator it = new BasicHeaderElementIterator(response.headerIterator(HTTP.CONN_KEEP_ALIVE));
  while (it.hasNext()) {
    HeaderElement he = it.nextElement();
    String param = he.getName();
    String value = he.getValue();
    if (value != null && param.equalsIgnoreCase("timeout")) {
      long timeout = Long.parseLong(value) * 1000;
      if (timeout > 20 * 1000) {
        return 20 * 1000;
      } else {
        return timeout;
      }
    }
  }
  return 5 * 1000;
}
 
开发者ID:osswangxining,项目名称:iotplatform,代码行数:19,代码来源:WebhookMsgHandler.java

示例13: getUrl

import org.apache.http.protocol.HTTP; //导入依赖的package包/类
/**
 * Cell取得した場合にatomフォーマットでレスポンスが返却されること. $format → なし Accept → application/atom+xml
 */
@Test
public final void acceptがatomでCell取得した場合にatomフォーマットでレスポンスが返却されること() {
    String url = getUrl(this.cellId);
    // $format なし
    // Acceptヘッダ application/atom+xml
    HashMap<String, String> headers = new HashMap<String, String>();
    headers.put(HttpHeaders.AUTHORIZATION, BEARER_MASTER_TOKEN);
    headers.put(HttpHeaders.ACCEPT, MediaType.APPLICATION_ATOM_XML);
    this.setHeaders(headers);

    PersoniumResponse res = this.restGet(url);

    assertEquals(HttpStatus.SC_OK, res.getStatusCode());
    this.responseHeaderMap.put(HTTP.CONTENT_TYPE, MediaType.APPLICATION_ATOM_XML);
    this.checkHeaders(res);
    // Etagのチェック
    assertEquals(1, res.getResponseHeaders(HttpHeaders.ETAG).length);
    res.bodyAsXml();
}
 
开发者ID:personium,项目名称:personium-core,代码行数:23,代码来源:ReadTest.java

示例14: parse

import org.apache.http.protocol.HTTP; //导入依赖的package包/类
/**
 * Returns a list of {@link NameValuePair NameValuePairs} as parsed from an
 * {@link HttpEntity}. The encoding is taken from the entity's
 * Content-Encoding header.
 * <p>
 * This is typically used while parsing an HTTP POST.
 *
 * @param entity
 *            The entity to parse
 * @throws IOException
 *             If there was an exception getting the entity's data.
 */
public static List <NameValuePair> parse (
        final HttpEntity entity) throws IOException {
    ContentType contentType = ContentType.get(entity);
    if (contentType != null && contentType.getMimeType().equalsIgnoreCase(CONTENT_TYPE)) {
        String content = EntityUtils.toString(entity, Consts.ASCII);
        if (content != null && content.length() > 0) {
            Charset charset = contentType.getCharset();
            if (charset == null) {
                charset = HTTP.DEF_CONTENT_CHARSET;
            }
            return parse(content, charset);
        }
    }
    return Collections.emptyList();
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:28,代码来源:URLEncodedUtils.java

示例15: httpPush

import org.apache.http.protocol.HTTP; //导入依赖的package包/类
private static String httpPush (ArrayList<NameValuePair> nameValuePairs,String action) throws Exception {
  	HttpPost httppost = new HttpPost(FLOWZR_API_URL + nsString + "/" + action + "/");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,HTTP.UTF_8));
      HttpResponse response;
      String strResponse;
response = http_client.execute(httppost);
      HttpEntity entity = response.getEntity();
      int code = response.getStatusLine().getStatusCode();
      BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent()));
strResponse = reader.readLine(); 				 			
      entity.consumeContent();
      if (code!=200) {
      	 throw new Exception(Html.fromHtml(strResponse).toString());
      }
return strResponse;    	    	
  }
 
开发者ID:tiberiusteng,项目名称:financisto1-holo,代码行数:17,代码来源:FlowzrSyncEngine.java


注:本文中的org.apache.http.protocol.HTTP类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。