最新消息:

http post : Content-Type

http admin 3350浏览 0评论

今天在使用http post(curl)获取一个数据的时候发现了问题,死活拿不到数据。获取永远为空。

场景是这样的:我向微信发起一个请求,QQ请求我的服务器,由我请求接口服务器。返回结果
但问题是,如果由微信直接请求接口服务器。一切正常。反而因为我做了一次中间层后。取不到数据了。
一点一点排查:
1、接口服务器的TransferMode是chunked。OK。我CURL换成1.0请求,结果还是空
2、换file_get_contents。。。一样是空。
3、排查微信请求的头。在我的服务器上把$_SERVER变量打印出来。结果。。意外的发现:Content-Type: text/xml,居然是这个?那我换成这个请求接口服务器试一下呢?居然真的成功了
遇到问题,果然是要一个个的排查啊。对方是java的服务器。可能会httprawbody做了一个强验证。所以。。。不象我们PHP可以拿到数据。先simplexml_load_string,不行?再json_decode,还不行?再用其他处理。。。

 

ajax post content-type

 

<html>
<head>

<script type="text/javascript">
var xmlhttp;
function test(url)
{
	var num = document.getElementById("num").value;
	xmlhttp=null;
	if (window.XMLHttpRequest)
	{// code for Firefox, Opera, IE7, etc.
		xmlhttp=new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}

	if (xmlhttp!=null)
	{
		xmlhttp.onreadystatechange=state_Change;
		xmlhttp.open("POST","b.php",true);
		xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		//表单提交必须用application/x-www-form-urlencoded,如果是type = file,则必须用multipart/form-data
		xmlhttp.send("num=aa");
		//当提交方式为get时,不用xmlhttp.setRequestHeader()
		//xmlhttp.open("GET","b.php?num="+num,true); 
		//xmlhttp.send(null);
	}
	else
	{
		alert("Your browser does not support XMLHTTP.");
	}
}

function state_Change()
{
	if (xmlhttp.readyState==4)
	{// 4 = "loaded"
		if (xmlhttp.status==200)
		{// 200 = "OK"
			document.getElementById('show').innerHTML=xmlhttp.responseText;
		}
		else
		{
			alert("Problem retrieving data:" + xmlhttp.statusText);
		}
	}
}

</script>
</head>
<body onload="test()">

<!--<input type="text" id="num">
<input type="button" onclick="test()" value="submit">-->
<div id="show"></div>

</body>

</html>

b.php

<?php
$num = $_REQUEST["num"];
echo $num;
?>

转载请注明:爱开源 » http post : Content-Type

您必须 登录 才能发表评论!