http请求的头信息字段没法添加?
时间: 2019-06-07来源:开源中国
前景提要
HDC调试需求开发(15万预算),能者速来!>>>
我在使用的是ionic CLI 4.8.0. 我想在发送http请求时带上cookie信息,用于登录状态保持。
我的做法是在http post请求的headers里面添加“Cookie”属性,值为字符串sessionid=xxxxxxxxxxxxxx.
ts文件的代码如下: (我使用的是Angular的HttpClient)
import {HttpClient, HttpParams} from '@angular/common/http';
import { HttpHeaders } from '@angular/common/http';
构造函数里面初始化对象public http: HttpClient
事务代码:
//设置header
const headers = new HttpHeaders().set("Access-Control-Request-Method", "POST")
.set("Access-Control-Request-Headers", "accept, content-type")
.set("Cookie", "sessionid=tlikemu4syawevedarpir2my3pii23we"); //在header信息里添加Cookie属性
let options = { // HttpClient的post方法的第三个参数是options
withCredentials:true,
headers:headers
}
this.http.post('http://my/url/', {}, options).subscribe( // 发送post请求,body没有字段
data => this.ProcessLoginResponse(JSON.stringify(data)) ,
err => this.ProcessLoginError(JSON.stringify(err))
)
但是服务器处理到该请求时,请求的headers里面没有这个Cookie,我使用wireshark抓包,发现请求包的headers里面确实没有Cookie字段。
Hypertext Transfer Protocol
POST /user/login/ HTTP/1.1\r\n
Host: 192.168.1.108:8000\r\n
Connection: keep-alive\r\n
Content-Length: 2\r\n
Accept: application/json, text/plain, */*\r\n
Origin: file://\r\n
x-wap-profile: http://wap1.huawei.com/uaprof/HUAWEI_SOPHIA_L07_UAProfile.xml\r\n
User-Agent: Mozilla/5.0 (Linux; Android 4.4.2; HUAWEI P7-L07 Build/HuaweiP7-L07) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/30.0.0.0 Mobile Safari/537.36\r\n
Content-Type: application/json\r\n
Accept-Encoding: gzip,deflate\r\n
Accept-Language: zh-CN,en-US;q=0.8\r\n
X-Requested-With: com.ionicframework.vp2342525\r\n
\r\n
[Full request URI: http://192.168.1.108:8000/user/login/]
[HTTP request 1/1]
[Response in frame: 233]
File Data: 2 bytes
我试了下将headers.set替换为headers.append,请求头部也没有看到添加的字段。
请问该怎么才能正确添加信息到http请求的header里面

科技资讯:

科技学院:

科技百科:

科技书籍:

网站大全:

软件大全:

热门排行