php、asp 发起post请求

来源:互联网 发布:匿名内部类java构造器 编辑:程序博客网 时间:2024/03/29 08:22

asp

使用MSXML2.XMLHTTP发出post请求
参考
现在不再流行,可今天需要写一个支持post的asp程序,好不容易找了个能用的。asp函数返回值就是赋值给一个和函数名相同的变量。

解决乱码问题,

'UTF-8:<%@Language="vbscript" Codepage="65001"%> <head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" > </head>`gb2312:<%@Language="vbscript" Codepage="936"%> <head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" ></head>
<%On error resume next '容错处理Function GetBody(ips) '获取远程IP地址POST信息Set https = Server.CreateObject("MSXML2.XMLHTTP") With https .Open "Post", "http://www.ip138.com/ips8.asp", False.setRequestHeader "Content-Type","application/x-www-form-urlencoded".Send "ip="&ips&"&action=2"GetBody = .ResponseBodyEnd With GetBody = BytesToBstr(GetBody,"GB2312")Set https = Nothing End FunctionFunction BytesToBstr(body,Cset) '转换GB2312/utf-8dim objstreamset objstream = Server.CreateObject("adodb.stream")objstream.Type = 1objstream.Mode =3objstream.Openobjstream.Write bodyobjstream.Position = 0objstream.Type = 2objstream.Charset = CsetBytesToBstr = objstream.ReadText objstream.Closeset objstream = nothingEnd FunctionResponse.Write GetBody("61.186.177.105")%>

php

curl 方式,这个直接使用库,但是有问题.
multipart form-data boundary 是被curl改为上传文件的形式。其中的bounty是分割开不同的上传项,随机产生

使用数组提供 post 数据时,CURL 组件大概是为了兼容 @filename 这种上传文件的写法,默认把 content_type 设为了 multipart/form-data。虽然对于大多数服务器并没有影响,但是还是有少部分服务器不兼容。

PHP中CURL的CURLOPT_POSTFIELDS参数使用细节

multipart form-data boundary 说明
POST multipart/form-data与application/x-www-form-urlencode的区别
http://blog.csdn.net/five3/article/details/7181521

<?phpheader("Content-Type:text/html;charset=utf-8");$ch = curl_init();/* 设置验证方式 */curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept:text/plain;charset=utf-8', 'Content-Type:application/x-www-form-urlencoded','charset=utf-8'));/* 设置返回结果为流 */curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);/* 设置超时时间*/curl_setopt($ch, CURLOPT_TIMEOUT, 10);/* 设置通信方式 */curl_setopt($ch, CURLOPT_POST, 1);curl_setopt ($ch, CURLOPT_URL, 'http://baidu.com/add.json');curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('apikey' => '24d20axxxxxxxxxxxxxxxxx', 'content' => ('您关注的#commodity_name#现在活动半价啦,赶快来看看吧!'))));$file_contents = curl_exec($ch);curl_close($ch);print_r($file_contents);?>

发起socket请求

function sock_post($url,$query){    $data = "";    $info=parse_url($url);    $fp=fsockopen($info["host"],80,$errno,$errstr,30);    if(!$fp){        return $data;    }    $head="POST ".$info['path']." HTTP/1.0\r\n";    $head.="Host: ".$info['host']."\r\n";    $head.="Referer: http://".$info['host'].$info['path']."\r\n";    $head.="Content-type: application/x-www-form-urlencoded\r\n";    $head.="Content-Length: ".strlen(trim($query))."\r\n";    $head.="\r\n";    $head.=trim($query);    $write=fputs($fp,$head);    $header = "";    while ($str = trim(fgets($fp,4096))) {        $header.=$str;    }    while (!feof($fp)) {        $data .= fgets($fp,4096);    }    return $data;}
0 0