我对tcl完全不熟悉。
我正在尝试向服务器发送帖子请求。这些是我要发送的标题:
"POST test HTTP/1.1"
"Content-Type:application/x-www-form-urlencoded"
"keep-alive:true"
"Authorization: Basic khjkjkhkhkfzxc"
"client: amin"
"Content-Length:10"
"ggggsis"
我的代码是:
set bs "Basic khjkjkhkhkfzxc"
set hd [list Authorization $bs]
set tk[http::geturl $url -headers $hd]
set res [http::data $tk]
http::cleanup $tk
return $tk
为什么这不起作用?我的错误在哪里?以及如何解决它?
非常感谢您的帮助。感谢
编辑:我创建了一个小型服务器套接字来显示我的请求,这是我发布的新代码:
set bs "Basic gfhfghfdhgf"
set hd [list Authorization $bs]
set token [http::geturl $url -headers $hd -query [::http::formatQuery blabla blablo] -type "text/xml"]
puts "done"
set postStat [http::status $token]
set postResp [http::data $token]
puts $postResp
puts "ok"
puts $postStat
以下是它的显示内容
Accepted connection from 127.0.0.1 at Fri Aug 1 10:51:22 EDT 2014
POST /QUEUE/test HTTP/1.0
Accept: */*
Host: localhost:9999
User-Agent: Tcl http client package 2.5.0
Authorization: Basic ZGVmYXVsdCJkZWZhdWx0
Content-Type: text/xml
Content-Length: 13
问题是程序发送帖子但是没有移出
set token [http::geturl $url -headers $hd -query [::http::formatQuery blabla blablo] -type "text/xml"],
永远不会发送httpget查询("完成"永远不会被打印,查询内容也不会在服务器端打印)
如果我解释得很糟糕,我很抱歉。
由于
答案 0 :(得分:0)
构建要作为字典发送的标头非常简单:
dict set hdr Authorization "Basic [binary encode base64 ${user}:${pass}]"
dict set hdr Client amin
您谈到的其他标题默认情况下是正确的(内容类型),或者最好留给http库来处理。
一旦您获得了标题和数据,就可以像这样发送POST
:
set token [http::geturl $theurl -headers $hdr -query $body]
set responseBody [http::data $token]
# Access headers here...
http::cleanup $token
# The token is no longer valid here
请注意,在调用http::cleanup
命令之前,您只能访问查询结果。之后,结果消失了(除非你将它们存储在其他地方的变量中)。
您最好确保$body
格式正确。 http::formatQuery
命令是为此而设计的:
set body [http::formatQuery fredKey fredValue daveKey daveValue foo bar]