use of org.apache.http.Header in project android_frameworks_base by ParanoidAndroid.
the class AndroidHttpClient method toCurl.
/**
* Generates a cURL command equivalent to the given request.
*/
private static String toCurl(HttpUriRequest request, boolean logAuthToken) throws IOException {
StringBuilder builder = new StringBuilder();
builder.append("curl ");
// add in the method
builder.append("-X ");
builder.append(request.getMethod());
builder.append(" ");
for (Header header : request.getAllHeaders()) {
if (!logAuthToken && (header.getName().equals("Authorization") || header.getName().equals("Cookie"))) {
continue;
}
builder.append("--header \"");
builder.append(header.toString().trim());
builder.append("\" ");
}
URI uri = request.getURI();
// relative URI. We want an absolute URI.
if (request instanceof RequestWrapper) {
HttpRequest original = ((RequestWrapper) request).getOriginal();
if (original instanceof HttpUriRequest) {
uri = ((HttpUriRequest) original).getURI();
}
}
builder.append("\"");
builder.append(uri);
builder.append("\"");
if (request instanceof HttpEntityEnclosingRequest) {
HttpEntityEnclosingRequest entityRequest = (HttpEntityEnclosingRequest) request;
HttpEntity entity = entityRequest.getEntity();
if (entity != null && entity.isRepeatable()) {
if (entity.getContentLength() < 1024) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
entity.writeTo(stream);
if (isBinaryContent(request)) {
String base64 = Base64.encodeToString(stream.toByteArray(), Base64.NO_WRAP);
builder.insert(0, "echo '" + base64 + "' | base64 -d > /tmp/$$.bin; ");
builder.append(" --data-binary @/tmp/$$.bin");
} else {
String entityString = stream.toString();
builder.append(" --data-ascii \"").append(entityString).append("\"");
}
} else {
builder.append(" [TOO MUCH DATA TO INCLUDE]");
}
}
}
return builder.toString();
}
use of org.apache.http.Header in project android_frameworks_base by ParanoidAndroid.
the class MultipartTest method testParts.
public void testParts() throws Exception {
StringBuffer filebuffer = new StringBuffer();
String filepartStr = "this is file part";
filebuffer.append(filepartStr);
File upload = File.createTempFile("Multipart", "test");
FileWriter outFile = new FileWriter(upload);
BufferedWriter out = new BufferedWriter(outFile);
try {
out.write(filebuffer.toString());
out.flush();
} finally {
out.close();
}
Part[] parts = new Part[3];
parts[0] = new StringPart("stringpart", "PART1!!");
parts[1] = new FilePart(upload.getName(), upload);
parts[2] = new StringPart("stringpart", "PART2!!");
MultipartEntity me = new MultipartEntity(parts);
ByteArrayOutputStream os = new ByteArrayOutputStream();
me.writeTo(os);
Header h = me.getContentType();
String boundry = EncodingUtils.getAsciiString(me.getMultipartBoundary());
StringBuffer contentType = new StringBuffer("multipart/form-data");
contentType.append("; boundary=");
contentType.append(boundry);
assertEquals("Multipart content type error", contentType.toString(), h.getValue());
final String CRLF = "\r\n";
StringBuffer output = new StringBuffer();
output.append("--");
output.append(boundry);
output.append(CRLF);
output.append("Content-Disposition: form-data; name=\"stringpart\"");
output.append(CRLF);
output.append("Content-Type: text/plain; charset=US-ASCII");
output.append(CRLF);
output.append("Content-Transfer-Encoding: 8bit");
output.append(CRLF);
output.append(CRLF);
output.append("PART1!!");
output.append(CRLF);
output.append("--");
output.append(boundry);
output.append(CRLF);
output.append("Content-Disposition: form-data; name=\"");
output.append(upload.getName());
output.append("\"; filename=\"");
output.append(upload.getName());
output.append("\"");
output.append(CRLF);
output.append("Content-Type: application/octet-stream; charset=ISO-8859-1");
output.append(CRLF);
output.append("Content-Transfer-Encoding: binary");
output.append(CRLF);
output.append(CRLF);
output.append(filepartStr);
output.append(CRLF);
output.append("--");
output.append(boundry);
output.append(CRLF);
output.append("Content-Disposition: form-data; name=\"stringpart\"");
output.append(CRLF);
output.append("Content-Type: text/plain; charset=US-ASCII");
output.append(CRLF);
output.append("Content-Transfer-Encoding: 8bit");
output.append(CRLF);
output.append(CRLF);
output.append("PART2!!");
output.append(CRLF);
output.append("--");
output.append(boundry);
output.append("--");
output.append(CRLF);
// System.out.print(output.toString());
assertEquals("Multipart content error", output.toString(), os.toString());
// System.out.print(os.toString());
}
use of org.apache.http.Header in project UltimateAndroid by cymcsg.
the class HttpUtils_Deprecated method GetJson_Cookie.
/**
* 处理httpResponse信息,返回json,保存cookie
*
* @param
* @return String
*/
public static String GetJson_Cookie(String httpUrl) {
String strResult = null;
try {
// HttpGet连接对象
HttpGet httpRequest = new HttpGet(httpUrl);
// 取得HttpClient对象
HttpClient httpclient = new DefaultHttpClient();
// 请求HttpClient,取得HttpResponse
HttpResponse httpResponse = httpclient.execute(httpRequest);
//保存cookie
StringBuffer sb = new StringBuffer();
String inputLine = "";
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
Header[] hds = httpResponse.getAllHeaders();
int isok = 0;
for (int index = 0; index < hds.length; index++) {
if ("Set-Cookie".equals(hds[index].getName())) {
String value = hds[index].getValue();
String[] vs = value.split(";");
for (int i = 0; i < vs.length; i++) {
String[] vss = vs[i].split("=");
if ("member".equals(vss[0])) {
rst = vs[i] + ";";
Log.d("Chen", "cookie信息:" + rst);
isok++;
}
}
}
}
}
// 请求成功
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
// 取得返回的字符串
strResult = EntityUtils.toString(httpResponse.getEntity());
} else {
Log.i(TAG, "请求错误");
}
} catch (Exception e) {
Log.i(TAG, "请求网络异常");
strResult = "net_ex";
}
return strResult;
}
use of org.apache.http.Header in project hive by apache.
the class HttpBasicAuthInterceptor method addHttpAuthHeader.
@Override
protected void addHttpAuthHeader(HttpRequest httpRequest, HttpContext httpContext) throws Exception {
Header basicAuthHeader = authScheme.authenticate(credentials, httpRequest, httpContext);
httpRequest.addHeader(basicAuthHeader);
}
use of org.apache.http.Header in project spark by perwendel.
the class SparkTestUtil method doMethod.
public UrlResponse doMethod(String requestMethod, String path, String body, boolean secureConnection, String acceptType, Map<String, String> reqHeaders) throws IOException {
HttpUriRequest httpRequest = getHttpRequest(requestMethod, path, body, secureConnection, acceptType, reqHeaders);
HttpResponse httpResponse = httpClient.execute(httpRequest);
UrlResponse urlResponse = new UrlResponse();
urlResponse.status = httpResponse.getStatusLine().getStatusCode();
HttpEntity entity = httpResponse.getEntity();
if (entity != null) {
urlResponse.body = EntityUtils.toString(entity);
} else {
urlResponse.body = "";
}
Map<String, String> headers = new HashMap<>();
Header[] allHeaders = httpResponse.getAllHeaders();
for (Header header : allHeaders) {
headers.put(header.getName(), header.getValue());
}
urlResponse.headers = headers;
return urlResponse;
}
Aggregations