我想知道响应某个http请求发送了多少数据.我目前做的是这样的:
HttpURLConnection con = (HttpURLConnection) feedurl.openConnection();
Run Code Online (Sandbox Code Playgroud)
//检查content-size的响应int feedsize = con.getContentLength();
问题是,内容 - legnth并不总是设置.例如,当服务器使用transfer-encoding = chunked时,我得到一个值-1.
我并不需要这显示进度信息.我只需要知道完成后发送给我的数据的大小.
背景:我需要这些信息,因为我想将它与使用gzip编码发送的响应大小进行比较.
背景 - 我正在尝试使用C#中的HttpWebRequest/HttpWebResponse将现有网页流式传输到单独的Web应用程序.我引人注目的一个问题是我正在尝试使用文件下载的内容长度来设置文件上载请求内容长度,但是当源网页位于HttpWebResponse不具有的Web服务器上时,问题似乎就出现了问题.提供内容长度.
HttpWebRequest downloadRequest = WebRequest.Create(new Uri("downloaduri")) as HttpWebRequest;
using (HttpWebResponse downloadResponse = downloadRequest.GetResponse() as HttpWebResponse)
{
var uploadRequest = (HttpWebRequest) WebRequest.Create(new Uri("uripath"));
uploadRequest.Method = "POST";
uploadRequest.ContentLength = downloadResponse.ContentLength; // ####
Run Code Online (Sandbox Code Playgroud)
问题:我如何更新此方法以满足此情况(当下载响应没有设置内容长度时).是不是以某种方式使用MemoryStream呢?任何示例代码将不胜感激. 特别是有一个代码示例,有人会说,如何进行"分块"HTTP下载和上传,以避免源Web服务器的任何问题不提供内容长度?
谢谢
似乎nginx不能很好地支持chunked请求.但我正在努力获得更明确(当前)的答案.我有一个客户端从Java客户端向服务器发出SOAP请求,该客户端设置标头Transfer-Encoding: chunked.当我直接连接到Tomcat上的应用程序时,一切正常.
但是当我把nginx放在它们之间时,事情就会破裂.
添加一些细节:我正在使用CloudFoundry.我正在使用Micro Cloud Foundry确认在没有nginx的情况下,事情按预期工作.但我的要求是使用cloudfoundry.com,所以我没有能力绕过那里的nginx.
这个问题和答案说这可能是我唯一的解决方法:http://wiki.nginx.org/NginxHttpChunkinModule.但是这种解决方法不可用,因为我无法修改cloudfoundry.com上的配置.
这个问题看起来也很相似,但它实际上涵盖了这一要求的相反方向.它涵盖了分块响应而不是分块请求.
那么客户端的任何变化如何解决这个问题呢?是否可以发送两个Transfer-Encoding: chunked和Content-Length: 123作为标题?这个领域对我来说是新的,但似乎像Apache HttpComponents这样的项目可以设置长度或分块但不是两者.分块的重点是您不需要知道请求开始时的长度.我可以告诉我的客户端使用HTTP/1.0并在没有分块的情况下使用nginx很好吗?我还忘记了其他解决方法吗?
如果我的HTTP服务器获得带有"Connection:keep-alive"标头的HTTP/1.0请求,那么客户是否理解"Transfer-Encoding:chunked"是否公平?
本质上,我正在尝试决定是否尊重HTTP/1.0客户端的"Connection:keep-alive"标头.如果我确实尊重它,那么我必须使用chunked编码进行回复,因为我无法缓冲整个回复以便计算Content-Length标头.
如果期望请求"Connection:keep-alive"的HTTP/1.0客户端也理解分块编码是不安全的,那么我将不得不在每次回复后关闭连接.(或者我错过了什么?)
http http-content-length chunked-encoding http-headers http-chunked
我有一个响应Twilio API的Java Servlet.似乎Twilio不支持我的回复使用的分块转移.我该如何避免使用Transfer-Encoding: chunked?
这是我的代码:
// response is HttpServletResponse
// xml is a String with XML in it
response.getWriter().write(xml);
response.getWriter().flush();
Run Code Online (Sandbox Code Playgroud)
我使用Jetty作为Servlet容器.
我正在实现一个java客户端,通过HTTP API将大文件上传到Akamai NetStorage版本4(对象存储).使用分块传输时,API要求在邮件正文的末尾发送尾随标头(预告片).
目前我使用Jersey 1.x和ApacheHttpClient.我找不到任何方法来为请求设置这些预告片.
我在这里找到了一个如何在ChunkedOutputStream中添加它们的例子(它们被命名为"页脚"):https: //github.com/andyburke/bitflood/blob/master/clients/java/src/ChunkedOutputStream.java 但我会必须破解整个Jersey客户端实现来支持这一点.
所以我正在寻找一个聪明的解决方案......任何想法?
我试图从Web API获取PDF文档,并希望在Angular App中显示.获取"无法加载PDF文档错误".我在" 角度应用程序 "帖子中跟随了" AngularJS:显示blob(.pdf) ".然而,我可以通过以下" 使用AngularJS从ASP.NET Web API方法下载文件 "文章成功下载相同的文件.
看起来我得到的文件是"Chunked Transfer Encoded".不知何故,当试图在角度应用程序中显示时,这不会被解码.请指教.
Web API代码:
HttpResponseMessage result = null;
var localFilePath = @"C:\Test.pdf";
if (!File.Exists(localFilePath))
{
result = Request.CreateResponse(HttpStatusCode.Gone);
}
else
{// serve the file to the client
result = Request.CreateResponse(HttpStatusCode.OK);
result.Content = new StreamContent(new FileStream(localFilePath, FileMode.Open, FileAccess.Read));
result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
result.Content.Headers.ContentDisposition.FileName = "Test.pdf";
result.Content.Headers.Add("x-filename", "Test.pdf");
}
return result;
Run Code Online (Sandbox Code Playgroud)
角度控制器:
myModule.controller("pdfviewerController", function ($scope, $http, $log, $sce) {
$http.post('/api/Sample/GetTestFile', {responseType:'arraybuffer'})
.success(function (response) {
var file …Run Code Online (Sandbox Code Playgroud) I am working on a Java application which has a built in HTTP server, at the moment the server is implemented using ServerSocketChannel, it listens on port 1694 for requests:
msvrCh = ServerSocketChannel.open();
msvrCh.socket().bind(new InetSocketAddress(mintPort));
msvrCh.configureBlocking(false);
Run Code Online (Sandbox Code Playgroud)
A thread is installed to manage requests and responses:
Thread thrd = new Thread(msgReceiver);
thrd.setUncaughtExceptionHandler(exceptionHandler);
thrd.start();
Run Code Online (Sandbox Code Playgroud)
The thread is quite simple:
Runnable msgReceiver = new Runnable() {
@Override
public void run() {
try{
while( !Thread.interrupted() ) {
//Sleep a short period between checks for …Run Code Online (Sandbox Code Playgroud) 我必须处理大量文件上传(超过4GB)。目前,我正在使用新的HttpClient。是否可以在Angular 4.3的新HttpClient模块中为分块上载设置选项?我找到了带有ng-file-upload模块的Angularjs解决方案。不幸的是,它不适用于角度4。谢谢您的帮助。
file-upload chunks chunked-encoding angular angular4-httpclient
这是server.xml我的tomcat 8中的配置.
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
Run Code Online (Sandbox Code Playgroud)
我只是发现一些RESTful api请求返回Transfer-Encoding: chunked标题,有些则没有.并且看起来它没有遵循我之前理解的规则:"内容大小超过阈值(比如8k)的响应将返回Transfer-Encoding: chunked"
所以我的问题是什么才是真正的触发条件 Transfer-Encoding: chunked
chunked-encoding ×10
java ×6
http ×5
http-chunked ×2
angular ×1
angularjs ×1
blob ×1
c# ×1
chunked ×1
chunks ×1
file-upload ×1
http-headers ×1
jetty ×1
nginx ×1
pdf ×1
servlets ×1
size ×1
soap ×1
streaming ×1
tomcat ×1