最近研究ASP 和康盛UCENTER通信,遇到了新的问题,
那就是在写uc_user_login等client端函数时 遇到 Access denied for agent changed 问题。
原因在于之前对UC与应用程序之间通信的校验原理没搞清楚的原因
故写博客备忘:
表忘记在http头提交需要的东东。
UC/model/base.php 中校验的http_user_agent是在模拟提交的过程中的当前客户浏览器的usera gent ,
如果在模拟提交过程中,没有将当前用户的useragent提交上去的话,那么UC在
uc/model/base.php 中的 init_input方法获取到的http_user_agent将会是应用服务器当前系统的默认user agent,
而不是客户端的http_user_agent,从而导致 http_user_agent 校验失败,而无法正常通信。
以下是PHP和ASP模拟提交的代码
PHP代码
- function uc_fopen($url, $limit = 0, $post = ”, $cookie = ”, $bysocket = FALSE, $ip = ”, $timeout = 15, $block = TRUE) {
- $return = ”;
- $matches = parse_url($url);
- !isset($matches['host']) && $matches['host'] = ”;
- !isset($matches['path']) && $matches['path'] = ”;
- !isset($matches['query']) && $matches['query'] = ”;
- !isset($matches['port']) && $matches['port'] = ”;
- $host = $matches['host'];
- $path = $matches['path'] ? $matches['path'].($matches['query'] ? ‘?’.$matches['query'] : ”) : ‘/’;
- $port = !emptyempty($matches['port']) ? $matches['port'] : 80;
- if($post) {
- $out = “POST $path HTTP/1.0\r\n”;
- $out .= “Accept: */*\r\n”;
-
- $out .= “Accept-Language: zh-cn\r\n”;
- $out .= “Content-Type: application/x-www-form-urlencoded\r\n”;
- $out .= “User-Agent: $_SERVER[HTTP_USER_AGENT]\r\n”;
- $out .= “Host: $host\r\n”;
- $out .= ‘Content-Length: ’.strlen($post).“\r\n”;
- $out .= “Connection: Close\r\n”;
- $out .= “Cache-Control: no-cache\r\n”;
- $out .= “Cookie: $cookie\r\n\r\n”;
- $out .= $post;
- } else {
- $out = “GET $path HTTP/1.0\r\n”;
- $out .= “Accept: */*\r\n”;
-
- $out .= “Accept-Language: zh-cn\r\n”;
- $out .= “User-Agent: $_SERVER[HTTP_USER_AGENT]\r\n”;
- $out .= “Host: $host\r\n”;
- $out .= “Connection: Close\r\n”;
- $out .= “Cookie: $cookie\r\n\r\n”;
- }
- $fp = @fsockopen(($ip ? $ip : $host), $port, $errno, $errstr, $timeout);
- if(!$fp) {
- return ”;
- } else {
- stream_set_blocking($fp, $block);
- stream_set_timeout($fp, $timeout);
- @fwrite($fp, $out);
- $status = stream_get_meta_data($fp);
- if(!$status['timed_out']) {
- while (!feof($fp)) {
- if(($header = @fgets($fp)) && ($header == “\r\n” || $header == “\n”)) {
- break;
- }
- }
-
- $stop = false;
- while(!feof($fp) && !$stop) {
- $data = fread($fp, ($limit == 0 || $limit > 8192 ? 8192 : $limit));
- $return .= $data;
- if($limit) {
- $limit -= strlen($data);
- $stop = $limit <= 0;
- }
- }
- }
- @fclose($fp);
- return $return;
- }
- }
因此,在ASP提交的时候也应该把这些头提交上去
ASP代码
- Private Function doPost(url,param)
- Dim https
- Set https = Server.CreateObject(“MSXML2.XMLHTTP”)
- https.Open “Post”, url, False
- useragent=http_user_agent()
- https.setRequestHeader “Accept”, “*/*”
- https.setRequestHeader “Accept-Language”,“zh-cn”
- https.setRequestHeader “Content-Type”,“application/x-www-form-urlencoded”
- https.setRequestHeader “User-Agent”, useragent
- https.setRequestHeader “Content-Length”,len(param)
- https.setRequestHeader “Connection”,“Close”
- https.setRequestHeader “Cache-Control”,“no-cache”
- https.Send param
- If https.readystate=4 Then
- doPost = https.ResponseBody
- doPost = BytesToBstr(doPost,UC_CHARSET)
- End If
- Set https = Nothing
- End Function
如果不提交上去,特别是user agent如果不提交上去,肯定就会出现:Access denied for agent changed 错误
Tags: UCenter, 整合