我正在使用此方法将json数据发布到php中的自制Web请求:
private string Post(string jsonContent)
{
var request = (HttpWebRequest)WebRequest.Create("http://localhost");
var encoding = new UTF8Encoding();
var byteArray = encoding.GetBytes(jsonContent);
request.ContentLength = byteArray.Length;
request.ContentType = @"application/json";
request.Method = "POST";
try
{
using (var dataStream = request.GetRequestStream())
{
dataStream.Write(byteArray, 0, byteArray.Length);
}
using (StreamReader responseStream = new StreamReader(((HttpWebResponse)request.GetResponse()).GetResponseStream()))
{
return responseStream.ReadToEnd();
}
}
catch(WebException ex)
{
Log(ex);
return string.Empty;
}
}
虽然这是php脚本 index.php
<?php
//Make sure that it is a POST request.
if(strcasecmp($_SERVER['REQUEST_METHOD'], 'POST') != 0)
{
var_dump($_SERVER['REQUEST_METHOD']);
throw new Exception('Request method must be POST!');
}
//Make sure that the content type of the POST request has been set to application/json
$contentType = isset($_SERVER["CONTENT_TYPE"]) ? trim($_SERVER["CONTENT_TYPE"]) : '';
if(strcasecmp($contentType, 'application/json') != 0)
{
var_dump($contentType);
throw new Exception('Content type must be: application/json');
}
//Receive the RAW post data.
$content = trim(file_get_contents("php://input"));
//Attempt to decode the incoming RAW post data from JSON.
$decoded = json_decode($content, true);
//If json_decode failed, the JSON is invalid.
if(!is_array($decoded)){
throw new Exception('Received content contained invalid JSON!');
}
//Process the JSON.
var_dump($decoded);
?>
当我现在调用类似var response = Post("{\"foo\":\"bar\"}");
的c#方法时,会导致错误&#39;请求方法必须是POST!&#39; 。因为它实际上是GET。
我正在使用WTServer在Windows 10上使用nginx.Nginx 1.11.4,PHP 7.0.11,这是回复:
<pre class='xdebug-var-dump' dir='ltr'>
<small>C:\WTServer\WWW\index.php:6:</small><small>string</small> <font color='#cc0000'>'GET'</font> <i>(length=3)</i>
</pre><br />
<font size='1'>
<table class='xdebug-error xe-uncaught-exception' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Fatal error: Uncaught Exception: Request method must be POST! in C:\WTServer\WWW\index.php on line <i>7</i></th></tr>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Exception: Request method must be POST! in C:\WTServer\WWW\index.php on line <i>7</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.2190</td><td bgcolor='#eeeeec' align='right'>360056</td><td bgcolor='#eeeeec'>{main}( )</td><td title='C:\WTServer\WWW\index.php' bgcolor='#eeeeec'>...\index.php<b>:</b>0</td></tr>
</table>
</font>