您的位置:首页 > 编程语言 > ASP

php、asp 发起post请求

2015-11-02 19:46 465 查看

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 = .ResponseBody
End With
GetBody = BytesToBstr(GetBody,"GB2312")
Set https = Nothing
End Function

Function BytesToBstr(body,Cset) '转换GB2312/utf-8
dim objstream
set objstream = Server.CreateObject("adodb.stream")
objstream.Type = 1
objstream.Mode =3
objstream.Open
objstream.Write body
objstream.Position = 0
objstream.Type = 2
objstream.Charset = Cset
BytesToBstr = objstream.ReadText
objstream.Close
set objstream = nothing
End Function

Response.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的区别

/article/7696495.html

<?php
header("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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: