本文整理汇总了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();
}
}
示例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;
}
示例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();
}
}
示例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;
}
示例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;
}
示例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;
}
示例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;
}
示例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();
}
}
示例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;
}
示例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();
}
}
示例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;
}
示例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;
}
示例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();
}
示例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();
}
示例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;
}