根据HTML4引用,没有使用anchor标记传递HTTP头的属性.
我想提供一个使用Accept标头请求特定文件类型的链接.
我能看到的唯一方法就是让它成为,并传递一个GET参数.
你可能为什么我想要这样做...我打算将一堆方法公开为公共API,将结果作为JSON.当使用JavaScript或其他编程语言Accept进行请求时,使用标头来请求特定的响应格式是"正确的方法".但是,这将意味着,我需要适应两个Accept头,并在GET在我的代码的参数,它闻起来像逻辑的重复.
这个主题在很大程度上是值得商榷的,因为这样的链接可能无法在浏览器中添加书签...仍然...我想知道是否有可能没有太多魔法......
假设我想在动态生成的页面上添加一个favicon(特别是一个ADF Faces页面,但这是无关紧要的).我无法修改我生成的html的标签.所以我尝试在servlet头中添加一个http-header.我希望我的标题与html head元素相同:
<link rel="shortcut icon" href="http://foo.com/favicon.ico">
Run Code Online (Sandbox Code Playgroud)
我是这样添加的:
httpResponse.addHeader("Link", "<http://foo.com/favicon.ico>; rel=\"shortcut icon\"");
Run Code Online (Sandbox Code Playgroud)
在我的浏览器中,我看到这个标题作为回应:
Link: <http://foo.com/favicon.ico>; rel="shortcut icon"
Run Code Online (Sandbox Code Playgroud)
但是,不幸的是,这对IE或Chrome没有任何影响.有人试图达到同样的目的吗?难道我做错了什么?那应该有用吗?
我有一个Android Phonegap/Cordova应用程序,它与ASP.NET MVC Web应用程序交互.我试图检测服务器端是否从浏览器或我的Phonegap/Cordova应用程序加载网页.
我考虑过几种方法:
我在用户代理字符串更改时做错了吗?或者有另一种方法可以实现这一目标吗?
如何设置http标头从我们的Android应用程序发送json对象 我们想用什么类型的标头从客户端发送数据到服务器.为什么我们使用标头,并在那个重要性.
public class HomeLayoutActivity extends Activity implements OnClickListener{
private EditText value;
private Button btn;
private ProgressBar pb;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_layout);
value=(EditText)findViewById(R.id.editText1);
btn=(Button)findViewById(R.id.button1);
pb=(ProgressBar)findViewById(R.id.progressBar1);
pb.setVisibility(View.GONE);
btn.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.home_layout, menu);
return true;
}
public void onClick(View v) {
// TODO Auto-generated method stub
if(value.getText().toString().length()<1){
// out of range
Toast.makeText(this, "please enter something", Toast.LENGTH_LONG).show();
}else{
pb.setVisibility(View.VISIBLE);
new MyAsyncTask().execute(value.getText().toString());
}
}
private class MyAsyncTask extends AsyncTask<String, Integer, Double>{
@Override
protected Double doInBackground(String... …Run Code Online (Sandbox Code Playgroud) 我有一个网页,当我访问材料时返回以下标题:
HTTP/1.1 200 OK
Date: Sat, 29 Jun 2013 15:57:25 GMT
Server: Apache
Content-Length: 2247515
Cache-Control: no-cache, no-store, must-revalidate, max-age=-1
Pragma: no-cache, no-store
Expires: -1
Connection: close
Run Code Online (Sandbox Code Playgroud)
使用chrome扩展,我想修改它,response header以便实际缓存材料而不是浪费带宽.
我有以下示例代码:
chrome.webRequest.onHeadersReceived.addListener(function(details)
{
// Delete the required elements
removeHeader(details.responseHeaders, 'pragma');
removeHeader(details.responseHeaders, 'expires');
// Modify cache-control
updateHeader(details.responseHeaders, 'cache-control', 'max-age=3600;')
console.log(details.url);
console.log(details.responseHeaders);
return{responseHeaders: details.responseHeaders};
},
{urls: ["<all_urls>"]}, ['blocking', 'responseHeaders']
);
Run Code Online (Sandbox Code Playgroud)
这正确地将标题修改为这样的东西(基于console.log()输出):
HTTP/1.1 200 OK
Date: Sat, 29 Jun 2013 15:57:25 GMT
Server: Apache
Content-Length: 2247515
Cache-Control: max-age=3600
Connection: close
Run Code Online (Sandbox Code Playgroud)
但基于我试图检查的所有内容,我无法看到任何证据确实发生了这种情况:
google-chrome httpresponse cache-control http-headers google-chrome-extension
我正在尝试使用HTTP/1.1 Partial Content标头将mp3文件流式传输到SoundManager,以便为我的媒体文件提供一些保护,并允许用户寻找轨道的不同位置.我在IE7-10,Firefox,Safari和Opera中使用的代码但拒绝在Google Chrome中运行.如果我要删除部分内容标题,它将播放该文件,但不允许用户搜索.
在检查Chrome开发工具中的网络选项卡时,有2个请求,其中一个处于挂起状态,另一个处于取消状态.这两个请求的大小都是13B.我要播放的文件是9.11MB.
下面是我用来设置标题和读取文件的代码.
$name = $_GET['name'];
$file_size = filesize($file);
$file = "C:\xampp\htdocs\..\protected\music\testsong.mp3"; // song, user and all other checks are performed before this to get the file path.
$etag = md5(uniqid(rand(), true));
$open_file = fopen($file, 'rb');
if( !$open_file ) throw new Exception('Could not open file');
$start = 0;
$end = $file_size - 1;
if( isset($_SERVER['HTTP_RANGE']) && !empty($_SERVER['HTTP_RANGE']) ) {
$range = explode('-', substr($_SERVER['HTTP_RANGE'], strlen('bytes=')));
$start = $range[0];
if( $range[1] > 0 ) $end = $range[1];
header('HTTP/1.1 206 …Run Code Online (Sandbox Code Playgroud) 我了解您可以通过以下方式提供静态内容:
app.use(express.static(__dirname + "../../../public_html"));
Run Code Online (Sandbox Code Playgroud)
但是,我试图根据响应发送的"Accept"标题明确更改它提供的内容的表示.通常,我通过REST API以JSON格式请求我拥有的内容,因此url是:http://blah.com/this/that/item并且运行良好.
但是,我还希望用户能够从浏览器访问同一页面,这将通过以下内容发送:Accept:text/html并且由于该标题,请查看具有正确格式(CSS/JS/HTML /等)的页面以呈现相同的信息.
现在,我正试图通过以下方式提供内容:
if (req.accepts("text/html")) {
res.sendfile("/", {
root: "../../../public_html"
});
res.status(200);
return;
}
Run Code Online (Sandbox Code Playgroud)
凡public_html持有index.html与CSS和JS的相对目录.我不会在完成后发送该文件,但我认为这将是一个良好的开端,然后在我弄清楚如何基于Accept标头提供静态内容之后添加JSON内容.
有一个更好的方法吗?
我想Content-type: application/json在我对Angular2中后端的所有请求中设置标题.我在我的主app.js文件中使用它.
let headers = new Headers({
'Content-Type', 'application/json'
})
class MyOptions extends BaseRequestOptions {
headers: headers
}
bootstrap(App, [
provide(RequestOptions, {useClass: MyOptions}),
ROUTER_BINDINGS,
HTTP_PROVIDERS,
bind(APP_BASE_HREF).toValue('/')
])
Run Code Online (Sandbox Code Playgroud)
我期望Http使用新内容类型的所有用途,但此代码仍设置为内容类型text/plain
saveMaster (master) {
return this.http
.put(`${config.API_URL}/masters/${master._id}`, JSON.stringify(master))
.map(res => res.json())
}
Run Code Online (Sandbox Code Playgroud)
我必须手动设置每个请求的标头,以使其正常工作.难道我做错了什么?
注意:我想要全局设置标题选项,而不必像每个请求类型一样设置它,就像在此解决方案中找到的那样.
我需要运行一个只能记录传入请求的简单HTTP服务器.
它应该记录整个请求的内容.像标题,饼干,身体....
我需要一个简单的解决方案,我可以在几分钟内运行,并将工作.
实施语言并不重要.
像查尔斯,但HTTP服务器而不是代理
edit2:https://stackoverflow.com/a/37784969/107282建议我不必担心,因为在真实设备上不会发生这种情况.
我在VueJS混合(Cordova)应用程序中使用Axios并且正在调用API.
Axios(正确地)OPTIONS在我的GET/ POST请求之前执行'预检' 请求,但是如果API 503在那时返回a ,则不会调用我的错误处理代码.
HTTP.post(url, data, {headers: {Authorization: 'Bearer ' + token.getAccessToken()}})
.then(response => response)
.catch(error => {
// Not reached
});Run Code Online (Sandbox Code Playgroud)
如何捕获此错误状态?
编辑:chrome dev工具的截图:
http-headers ×10
android ×2
html ×2
http ×2
angular ×1
axios ×1
content-type ×1
cordova ×1
express ×1
favicon ×1
httpresponse ×1
httpserver ×1
java ×1
javascript ×1
json ×1
logging ×1
node.js ×1
php ×1
rest ×1
streaming ×1
user-agent ×1
vue.js ×1