RequestBody requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("uploaded_file", filename, RequestBody.create(MEDIA_TYPE_PNG, sourceFile))
.addFormDataPart("flowChunkNumber", "1")
.addFormDataPart("flowCurrentChunkSize", String.valueOf(sourceFile.getTotalSpace()))
.addFormDataPart("flowChunkSize", "1048576")
.addFormDataPart("flowIdentifier", "4731-images1jpeg")
.addFormDataPart("flowFilename", "images (1).jpeg")
.addFormDataPart("flowFilename", "images (1).jpeg")
.addFormDataPart("flowRelativePath", "images (1).jpeg")
.addFormDataPart("flowTotalChunks", "1")
.build();
Request request = new Request.Builder()
.addHeader("cookie", ******* )
.url(URL_UPLOAD_IMAGE)
.post(requestBody)
.build();
HttpLoggingInterceptor logInterceptor = new HttpLoggingInterceptor();
logInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient
.Builder()
.addNetworkInterceptor(logInterceptor)
.build();
Response response = client.newCall(request).execute();
D/OKHTTP:主机:www.appido.ir
D/OKHTTP:Connection:Keep-Alive
d/okhttp:accept-encoding:gzip
1048576------WebKitFormBoundaryJDHM3Si8Enjzaba内容-配置:表单-数据;name=“FlowCurrentChunkSize”
23016-----WebKitFormBoundaryJDHM3Si8Enjzaba内容-配置:表单-数据;name=“FlowTotalSize”
23016-----WebKitFormBoundaryJDHM3Si8Enjzaba内容-配置:表单-数据;name=“flowidentifier”
----WebKitFormBoundaryJDHM3Si8Enjzaba--
它似乎是日志拦截器中的一个bug。
我能够解决这个问题,购买调试日志拦截器。问题是多个部分的体太大,迭代器不能一次显示这么大的体。需要逐行打印。以下是在okhttp 3.4.1中使用多部分的日志拦截器的修改版本:
final class HttpLoggingInterceptor2 implements Interceptor {
private static final Charset UTF8 = Charset.forName("UTF-8");
public enum Level {
/**
* No logs.
*/
NONE,
/**
* Logs request and response lines.
* <p/>
* <p>Example:
* <pre>{@code
* --> POST /greeting http/1.1 (3-byte body)
*
* <-- 200 OK (22ms, 6-byte body)
* }</pre>
*/
BASIC,
/**
* Logs request and response lines and their respective headers.
* <p/>
* <p>Example:
* <pre>{@code
* --> POST /greeting http/1.1
* Host: example.com
* Content-Type: plain/text
* Content-Length: 3
* --> END POST
*
* <-- 200 OK (22ms)
* Content-Type: plain/text
* Content-Length: 6
* <-- END HTTP
* }</pre>
*/
HEADERS,
/**
* Logs request and response lines and their respective headers and bodies (if present).
* <p/>
* <p>Example:
* <pre>{@code
* --> POST /greeting http/1.1
* Host: example.com
* Content-Type: plain/text
* Content-Length: 3
*
* Hi?
* --> END POST
*
* <-- 200 OK (22ms)
* Content-Type: plain/text
* Content-Length: 6
*
* Hello!
* <-- END HTTP
* }</pre>
*/
BODY
}
public interface Logger {
void log(String message);
/**
* A {@link Logger} defaults output appropriate for the current platform.
*/
Logger DEFAULT = new Logger() {
@Override
public void log(String message) {
Platform.get().log(INFO, message, null);
}
};
}
public HttpLoggingInterceptor2() {
this(Logger.DEFAULT);
}
public HttpLoggingInterceptor2(Logger logger) {
this.logger = logger;
}
private final Logger logger;
private volatile Level level = Level.NONE;
/**
* Change the level at which this interceptor logs.
*/
public HttpLoggingInterceptor2 setLevel(Level level) {
if (level == null) throw new NullPointerException("level == null. Use Level.NONE instead.");
this.level = level;
return this;
}
public Level getLevel() {
return level;
}
@Override
public Response intercept(Chain chain) throws IOException {
Level level = this.level;
Request request = chain.request();
if (level == Level.NONE) {
return chain.proceed(request);
}
boolean logBody = level == Level.BODY;
boolean logHeaders = logBody || level == Level.HEADERS;
RequestBody requestBody = request.body();
boolean hasRequestBody = requestBody != null;
Connection connection = chain.connection();
Protocol protocol = connection != null ? connection.protocol() : Protocol.HTTP_1_1;
String requestStartMessage = "--> " + request.method() + ' ' + request.url() + ' ' + protocol;
if (!logHeaders && hasRequestBody) {
requestStartMessage += " (" + requestBody.contentLength() + "-byte body)";
}
logger.log(requestStartMessage);
if (logHeaders) {
if (hasRequestBody) {
// Request body headers are only present when installed as a network interceptor. Force
// them to be included (when available) so there values are known.
if (requestBody.contentType() != null) {
logger.log("Content-Type: " + requestBody.contentType());
}
if (requestBody.contentLength() != -1) {
logger.log("Content-Length: " + requestBody.contentLength());
}
}
Headers headers = request.headers();
for (int i = 0, count = headers.size(); i < count; i++) {
String name = headers.name(i);
// Skip headers from the request body as they are explicitly logged above.
if (!"Content-Type".equalsIgnoreCase(name) && !"Content-Length".equalsIgnoreCase(name)) {
logger.log(name + ": " + headers.value(i));
}
}
if (!logBody || !hasRequestBody) {
logger.log("--> END " + request.method());
} else if (bodyEncoded(request.headers())) {
logger.log("--> END " + request.method() + " (encoded body omitted)");
} else {
Buffer buffer = new Buffer();
requestBody.writeTo(buffer);
Charset charset = Charset.forName("UTF8");// UTF8;
MediaType contentType = requestBody.contentType();
if (contentType != null) {
charset = contentType.charset(charset);
}
if (isPlaintext(buffer)) {
String string = buffer.clone().readString(charset);
String[] strings = string.split("\\r?\\n");
for (String subStr : strings) {
logger.log(subStr);
}
logger.log("--> END " + request.method()
+ " (" + requestBody.contentLength() + "-byte body)");
} else {
logger.log("--> END " + request.method() + " (binary "
+ requestBody.contentLength() + "-byte body omitted)");
}
}
}
long startNs = System.nanoTime();
Response response;
try {
response = chain.proceed(request);
} catch (Exception e) {
logger.log("<-- HTTP FAILED: " + e);
throw e;
}
long tookMs = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNs);
ResponseBody responseBody = response.body();
long contentLength = responseBody.contentLength();
String bodySize = contentLength != -1 ? contentLength + "-byte" : "unknown-length";
logger.log("<-- " + response.code() + ' ' + response.message() + ' '
+ response.request().url() + " (" + tookMs + "ms" + (!logHeaders ? ", "
+ bodySize + " body" : "") + ')');
if (logHeaders) {
Headers headers = response.headers();
for (int i = 0, count = headers.size(); i < count; i++) {
logger.log(headers.name(i) + ": " + headers.value(i));
}
if (!logBody || !HttpHeaders.hasBody(response)) {
logger.log("<-- END HTTP");
} else if (bodyEncoded(response.headers())) {
logger.log("<-- END HTTP (encoded body omitted)");
} else {
BufferedSource source = responseBody.source();
source.request(Long.MAX_VALUE); // Buffer the entire body.
Buffer buffer = source.buffer();
Charset charset = UTF8;
MediaType contentType = responseBody.contentType();
if (contentType != null) {
try {
charset = contentType.charset(UTF8);
} catch (UnsupportedCharsetException e) {
logger.log("");
logger.log("Couldn't decode the response body; charset is likely malformed.");
logger.log("<-- END HTTP");
return response;
}
}
if (!isPlaintext(buffer)) {
logger.log("");
logger.log("<-- END HTTP (binary " + buffer.size() + "-byte body omitted)");
return response;
}
if (contentLength != 0) {
logger.log("");
logger.log(buffer.clone().readString(charset));
}
logger.log("<-- END HTTP (" + buffer.size() + "-byte body)");
}
}
return response;
}
/**
* Returns true if the body in question probably contains human readable text. Uses a small sample
* of code points to detect unicode control characters commonly used in binary file signatures.
*/
static boolean isPlaintext(Buffer buffer) {
try {
Buffer prefix = new Buffer();
long byteCount = buffer.size() < 64 ? buffer.size() : 64;
buffer.copyTo(prefix, 0, byteCount);
for (int i = 0; i < 16; i++) {
if (prefix.exhausted()) {
break;
}
int codePoint = prefix.readUtf8CodePoint();
if (Character.isISOControl(codePoint) && !Character.isWhitespace(codePoint)) {
return false;
}
}
return true;
} catch (EOFException e) {
return false; // Truncated UTF-8 sequence.
}
}
private boolean bodyEncoded(Headers headers) {
String contentEncoding = headers.get("Content-Encoding");
return contentEncoding != null && !contentEncoding.equalsIgnoreCase("identity");
}
}
我正在研究SSO的一些新用法。基本上,我正在尝试找到如何拦截SAML请求的方法,该请求通过某种IdP代理或第三方服务从服务提供商发送到身份提供商,该服务将保存SAML请求并为用户提供一些附加功能。所需的过程可能如下所示: 用户从SP调用SAML请求-例如单击登录按钮 用户被重定向到第3方服务,例如,小型调查(这是理论示例) 提交调查后,用户被重定向到IdP并应继续登录 我对SimpleSAMLph
本文向大家介绍SpringBoot拦截器实现登录拦截的方法示例,包括了SpringBoot拦截器实现登录拦截的方法示例的使用技巧和注意事项,需要的朋友参考一下 源码 GitHub:https://github.com/291685399/springboot-learning/tree/master/springboot-interceptor01 SpringBoot拦截器可以做什么 可以对UR
本文向大家介绍Spring MVC 拦截器实现登录,包括了Spring MVC 拦截器实现登录的使用技巧和注意事项,需要的朋友参考一下 上篇博文我在博客中讲到如何使用spring MVC框架来实现文件的上传和下载,今天小钱给大家再来分享和介绍Spring MVC框架中相当重要的一块功能——拦截器。 关于拦截器的概念我在这里就不多说了,大家可以上网百度或者看别人写的具体博客,我今天要说的是拦截器在实
问题内容: 我们的应用程序要求用户登录才能查看任何内容。如果没有有效的用户会话,则会拦截对所有页面的访问,并弹出登录表单。我希望拦截器在显示登录表单之前记住原始请求URI,并在登录表单验证成功后重定向到该请求。我尝试按照Struts2Redirect进行身份验证拦截器后的更正操作 但是,由于,我得到了空白页。 从未被调用。 解决方案: 完全擦除@Results批注,而不是调用 问题答案: 如果未登
本文向大家介绍springmvc拦截器登录验证示例,包括了springmvc拦截器登录验证示例的使用技巧和注意事项,需要的朋友参考一下 一开始,学了拦截器与过滤器,咋一看两者有点像,实际上两者有很大的不同。就用拦截器和过滤器分别做了登录验证试验,这次先说拦截器。下面是自己实践的一个实例: 在spring-mvc.xml中配置拦截器: 如上所示,这里配置了LoginIntercepter,为了简单起
我以为这些最新版本应该是兼容的。有一条推文;https://twitter.com/JakeWharton/status/553066921675857922和Retrofit 1.9的更新日志也提到了它。 然而,当我尝试这个: 还是不行。setClient方法抱怨不兼容的客户端对象; 我错过了什么?我还看到OkHttpClient没有实现客户端接口。 我现在使用这种方法;https://medi