我有一个网站,我每月大约一次更新内容.当我检查HTTP请求标头字段时,我得到以下输出:
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Run Code Online (Sandbox Code Playgroud)
我的问题是,考虑到我更新内容的频率,我正在考虑手动设置这些字段以允许缓存网站.我正在使用php header(); 命令这样做.
因此,我的问题是:我的expours,cache-control和pragma HTTP请求头字段应该设置为什么?另外,我应该设置除此之外的任何其他字段吗?
我正在使用ASP.NET.我要么添加或设置一个cookie(取决于是否HttpRequest包含具有指定密钥的cookie),然后立即调用Response.Redirect.cookie未设置.这是正确的行为吗?在具有302状态代码的http响应期间设置cookie是否相互排斥?
if (context.HttpContext.Request.Browser.Cookies)
{
var cookies = context.HttpContext.Request.Cookies;
var stateCookie = new HttpCookie(SR.session, clientState.SessionId.ToString());
if (cookies.AllKeys.Contains(SR.session))
{
context.HttpContext.Response.Cookies.Set(stateCookie);
}
else
{
context.HttpContext.Response.Cookies.Add(stateCookie);
}
}
Run Code Online (Sandbox Code Playgroud)
asp.net response.redirect httpresponse httpcontext httpcookie
Android客户端向服务器发送请求.
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://test.com/test");
HttpResponse response = httpclient.execute(httppost);
Run Code Online (Sandbox Code Playgroud)
然后收到服务器的响应,如下所示
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Date: Wed, 26 Sep 2012 10:59:35 GMT
Content-Type: multipart/form-data; type="image/jpeg"; boundary="simple_boundary"; start="<image>"; start-info="image/jpeg"
Transfer-Encoding: chunked
2000
--simple_boundary
Content-Type: image/jpeg
Content-Transfer-Encoding: binary
Content-ID: <image>
......JPEG Binary Code Here.....
--simple_boundary
Run Code Online (Sandbox Code Playgroud)
如何从响应中获取图像(二进制).
InputStream is = response.getEntity().getContent();
Run Code Online (Sandbox Code Playgroud)
(InputStream)也包含边界和内容信息.
--simple_boundary
Content-Type: image/jpeg
Content-Transfer-Encoding: binary
Content-ID: <image>
......JPEG Binary Code Here.....
--simple_boundary
Run Code Online (Sandbox Code Playgroud)
如何获得纯Image二进制数据.它有可能??
我正在研究一个项目,其中"我必须从"服务器"获得4096字节的数据到服务器"每"1毫秒到10毫秒".但它"花费太多时间"即"大约300毫秒 - 700毫秒" "这导致我的应用程序丢失数据.
我正在使用下面的代码片段
HttpClient client = new DefaultHttpClient();
HttpPost request = new HttpPost("http://192.168.1.40/ping");
HttpResponse response = client.execute(request);
Run Code Online (Sandbox Code Playgroud)
HttpResponse只花费太多时间,即大约300ms - 700ms.
我怎样才能更快地得到回应?
而不是这个我可以使用什么来更快地从服务器获得响应呢?
请让我知道任何解决方案或解决方法.
我已经完成谷歌,经历过其他方式DataOutputStream,ByteOutputStream但没有使用这个,它也花了太多时间HttpResponse.
帮助将不胜感激.
我们如何停止服务器的剩余响应 - 例如.
http.get(requestOptions, function(response){
//Log the file size;
console.log('File Size:', response.headers['content-length']);
// Some code to download the remaining part of the response?
}).on('error', onError);
Run Code Online (Sandbox Code Playgroud)
我只想记录文件大小而不是浪费我的带宽来下载剩余的文件.nodejs是自动处理这个还是我必须为它编写一些特殊代码?
在我的WebAPI项目中,我遇到了重定向问题.这是因为Uri.ToString()方法以"防御"的方式运行,换句话说,一旦提到方法被调用,他就会解码查询字符串的安全部分.
考虑这个失败的单元测试:
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace UriTest
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
// Arrange
const string expectedUrlRaw =
"http://localhost/abc?proxy=http%3A%2F%2Ftarget.nl%3Fparam1%3Dvalue1%26param2%3Dvalue2";
const string expectedUrlInHttpsRaw =
"https://localhost/abc?proxy=http%3A%2F%2Ftarget.nl%3Fparam1%3Dvalue1%26param2%3Dvalue2";
Uri expectedUri = new Uri(expectedUrlRaw);
Uri expectedUriInHttps = new Uri(expectedUrlInHttpsRaw);
// Act
string returnsUriInHttpsRaw = expectedUri.ToHttps().ToString();
// Assert
Assert.AreEqual(expectedUrlInHttpsRaw, returnsUriInHttpsRaw);
}
}
public static class StringExtensions
{
public static Uri ToHttps(this Uri uri)
{
UriBuilder uriBuilder = new UriBuilder(uri);
uriBuilder.Scheme = Uri.UriSchemeHttps;
uriBuilder.Port = 443;
return uriBuilder.Uri;
}
} …Run Code Online (Sandbox Code Playgroud) 在我的应用程序中,我一直在考虑从服务器实现5xx响应的最佳方法.
第一种方法是编写我自己的Request.deliverError附加方法版本:
@Override
public void deliverError(VolleyError error) {
if(error.networkResponse == null){
super.deliverError(error);
return;
}else {
switch(error.networkResponse.statusCode){
case HttpStatus.SC_HTTP_VERSION_NOT_SUPPORTED:
AppInfo.reportDevInfo(GlideApplication.applicationContext, "got a 505 response for request" +this.toString(), null);
break;
case HttpStatus.SC_INTERNAL_SERVER_ERROR:
case HttpStatus.SC_BAD_GATEWAY:
case HttpStatus.SC_SERVICE_UNAVAILABLE:
case HttpStatus.SC_GATEWAY_TIMEOUT:
int retryCount = RETRY_COUNT - getRetryPolicy().getCurrentRetryCount();
if(retryCount < 0) {
super.deliverError(error);
return;
}
String backoff = error.networkResponse.getHeaders.get("Retry-After");
if(TextUtils.isEmpty(backoof) == false) {
attemptRetryWithNewBackoff(backoff);
return;
}
break;
}
super.deliverError(error)
}
}
}
Run Code Online (Sandbox Code Playgroud)
但这只会在应用程序中引起ANR.
看看进一步的研究,我发现这篇博文显示了一种处理不同响应代码的方法,唯一的问题是,我不确定如何将其推广到我的整个应用程序并使用适当的方法实现5xx响应代码的处理"Retry-After"heade.
在ErrorListener构造函数中创建一个实现并获取另一个类作为参数的类似乎非常昂贵且效率低下:
public class MyErrorListener implements ErrorListener …Run Code Online (Sandbox Code Playgroud) 我已经使用NanoHttpd在本地网络上成功实现了文件传输.但是,我无法在NanoHttpd Response中发送文件名.收到的文件具有如下默认名称:localhost_8080.我尝试在响应标头中附加文件名Content-disposition,但我的文件传输失败了.我究竟做错了什么?这是我的实现:
private class WebServer extends NanoHTTPD {
String MIME_TYPE;
File file;
public WebServer() {
super(PORT);
}
@Override
public Response serve(String uri, Method method,
Map<String, String> header, Map<String, String> parameters,
Map<String, String> files) {
try {
file=new File(fileToStream);
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
MIME_TYPE= URLConnection.guessContentTypeFromName(file.getName());
} catch (IOException ioe) {
Log.w("Httpd", ioe.toString());
}
NanoHTTPD.Response res=new NanoHTTPD.Response(Status.OK, MIME_TYPE, bis);
res.addHeader("Content-Disposition: attachment; filename=", file.getName());
return res;
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助!
我正在使用Retrofit 2.0.1.我想处理所有类型的网络错误和异常(如没有网络连接,超时错误,服务器未找到等)我找到了这个链接.但是在v1.8.0中已经弃用了一些方法.我怎么能在2.0.1中这样做?
我正在使用Spring REST控制器,特别是在异常处理程序上。异常处理程序按预期工作,并且我的JUnit-Test(使用Spring HTTP客户端)显示在客户端接收到正确的HTTP状态代码(400)。HTTP客户端会自动将其转换为HttpClientErrorException。
但是,HttpClientErrorException始终打印总是为我产生以下结果:
HttpClientErrorException: 400 null
... null部分是让我担心的。这不是服务器端异常的消息应该存在的地方吗?
我检查了HTTP客户端的源代码,以查看客户端异常在何处引发。看起来像这样:
throw new HttpClientErrorException(statusCode, response.getStatusText(), response.getHeaders(), getResponseBody(response), getCharset(response));
调试此调用后发现这response.getStatusText()就是null我的情况。
我的问题是:如何ResponseEntity在服务器端进行设计,以使HTTP客户端找到服务器端的异常消息response.getStatusText()而不是null?
我的当前看起来像这样:
@ExceptionHandler({ MyCustomException.class })
public ResponseEntity<String> handleException(final HttpServletRequest req, final MyCustomException e) {
HttpHeaders headers = new HttpHeaders();
headers.set("Content-type", "text/plain");
String body = e.toString();
return new ResponseEntity<>(body, headers, HttpStatus.BAD_REQUEST);
}
Run Code Online (Sandbox Code Playgroud)
...,然后输入null客户端状态文本。
httpresponse ×10
android ×4
java ×3
http-headers ×2
rest ×2
asp.net ×1
c# ×1
filenames ×1
http ×1
httpcontext ×1
httpcookie ×1
httprequest ×1
multipart ×1
nanohttpd ×1
node.js ×1
php ×1
retrofit2 ×1
spring ×1
uri ×1