我似乎无法弄清楚如何将标头添加到我的restlet响应中.当我看到在可用的方法Response的对象,我看到的是setStatus,setEntity和setAttributes但这些都不告诉我如何设置自定义HTTP头的响应.
例如,我有一个GET调用返回类似如下的内容:
HTTP/1.1 200 OK
Content-Type: text/json
Content-Length: 123
Some-Header: the value
Some-Other-Header: another value
{
id: 111,
value: "some value this could be anything",
diagnosis: {
start: 12552255,
end: 12552261,
key: "ABC123E11",
source: "S1",
}
}
Run Code Online (Sandbox Code Playgroud)
不管它是什么.在handleGet方法中,我这样处理它:
final MediaType textJsonType = new MediaType("text/json");
@Override
public void handleGet() {
log.debug("Handling GET...");
final Response res = this.getResponse();
try {
final MyObject doc = this.getObj("hello", 1, "ABC123E11", "S1");
final String …Run Code Online (Sandbox Code Playgroud) 有一个需要通过自定义http标头获取信息的四方派对应用程序,因此我编写了一个简单的测试应用程序,创建此标题,然后重定向到列出所有标题的页面.
生成标头的servlet片段是:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/plain");
response.setHeader("cust-header", "cust-val");
response.sendRedirect("header.jsp");
}
Run Code Online (Sandbox Code Playgroud)
另一方面,header.jsp的相关代码是:
<%
Enumeration enumeration = request.getHeaderNames();
while (enumeration.hasMoreElements()) {
String string = (String)enumeration.nextElement();
out.println("<font size = 6>" +string +": " + request.getHeader(string)+ "</font><br>");
}
%>
Run Code Online (Sandbox Code Playgroud)
这会显示以下标题:
Host: localhost:9082
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; es-ES; rv:1.9.2.10) Gecko/20100914 Firefox/3.6.10 (.NET CLR 3.5.30729)
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: es-es,es;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Referer: http://localhost:9082/HdrTest/login.jsp
Cookie: JSESSIONID=0000tubMmZOXDyuM4X9RmaYYTg4:-1
Run Code Online (Sandbox Code Playgroud)
好像从未插入自定义标头.我该如何解决?
谢谢
我试图使用C#修改HTTP标头.我试图在Page preinit事件上操作Request.Headers.但是当我尝试向Headers设置任何内容时,我得到PlatformNotSupportedException.由于我们无法将新的NameValueCollection设置为Reqeust.Headers,因此我尝试使用以下代码设置该值:
Request.Headers.Set(HttpRequestHeader.UserAgent.ToString(), "some value");
Run Code Online (Sandbox Code Playgroud)
不知道如何实现这一目标?
我有来自远程服务器的HTTP :: Response Perl对象的问题,有时会返回带有重复的"Content-Length"标头的HTTP响应.
发生这种情况时,如果内容长度值为'43215',那么当我读取标题值时:
print ($response->header('Content-length'));
Run Code Online (Sandbox Code Playgroud)
结果是:
4321543215
Run Code Online (Sandbox Code Playgroud)
如何检测标头是否重复并访问实际值?
我有兴趣使用target = _blanks作为链接,但由于我不想做参数化的URI,我担心丢失基于请求的数据,例如referrer.那种恐惧是错位的吗?
我有一个托管在服务器上的多语言网站,该服务器似乎将字符编码设置为默认为iso-8859-1.
我认为我最好拥有utf-8页面并包含一个meta标签来声明这一点.不幸的是,这个元标记似乎被覆盖,页面默认为iso-8859.
德语和荷兰语页面中的许多特殊字符无法正确显示.
我是否需要尝试将服务器默认更改为utf-8或其他内容?也许我可以完全删除服务器默认值?嗯......真的不确定这里最好做什么.
任何建议都会很棒!
我找不到任何让我可以访问GWT中的referrer头的类/方法.有人知道吗?
我有一个CI应用程序,具有auth控制器和switchuser功能.基本上它只需要从URI获取ID,从ID获取一些用户数据,分配一些会话数据然后加载视图.
function switch_user(){
if(!$this->auth_lib->is_admin()){
$this->session->set_flashdata('status', 'You need to be an administrator to access this page.');
redirect('auth/login');
}
if($id = $this->uri->segment(3)):
$this->load->model('auth_model', 'auth');
$username = $this->auth->get_username($id)->account_name;
$this->session->set_userdata(array('at_id' => $id, 'username' => $username));
$this->session->set_flashdata('status','User switched.');
endif;
$this->load->model('clients_model', 'clients');
$data['users'] = $this->clients->get_at_clients();
$this->load->view('auth/switch', $data);
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
Message: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\cp\application\controllers\auth.php:130)
Run Code Online (Sandbox Code Playgroud)
第130行是$ username = $ this-> auth-> get_username($ id) - > account_name;
这是模型功能:
function get_username($at_id){
$portal = $this->load->database('warehouse', TRUE);
$portal->select('account_name');
$portal->where('account_id', $at_id);
$portal->from('wh_account');
$query …Run Code Online (Sandbox Code Playgroud) 一直在乱砍几个库,并遇到一个字符串被"双重逃脱"的问题.
例如: 固定示例
> x = ['a']
=> ["a"]
> x.to_s
=> "[\"a\"]"
>
Run Code Online (Sandbox Code Playgroud)
然后再来
\"\[\\\"s\\\"\]\"
Run Code Online (Sandbox Code Playgroud)
在处理http标头时发生了这种情况.我有一个标题,它将是一个数组,但http库正在对array.to_s值进行自己的字符转义.
我发现的解决方法是将数组转换为字符串,然后"撤消"to_s.像这样:formatted_value = value.to_s
if value.instance_of?(Array)
formatted_value = formatted_value.gsub(/\\/,"") #remove backslash
formatted_value = formatted_value.gsub(/"/,"") #remove single quote
formatted_value = formatted_value.gsub(/\[/,"") #remove [
formatted_value = formatted_value.gsub(/\]/,"") #remove ]
end
value = formatted_value
Run Code Online (Sandbox Code Playgroud)
......有一个更好的方法......(不需要修补我正在使用的宝石).(是的,如果我的字符串实际上包含那些字符串,那么这个休息.)
建议?
**更新2**
好的.在这个街区仍然有麻烦,但现在我想我已经找到了核心问题.在to_s调用之后,它将我的数组序列化为json.至少,这似乎正在复制我所看到的.
[ 'A'].to_s.to_json
我正在调用一个返回to_s结果的gem中的方法,然后我在它上面调用to_json.