在将Json字符串发布到PHP Web服务器时遇到异常奇怪的行为.我使用JsonTextWriter对象来创建Json字符串.然后我将Json字符串作为POST请求发送.请参阅评论.代码中的HTML响应返回正确的输出,但在浏览器中查看时,网页显示NULL或array(0){}.
private void HttpPost(string uri, string parameters)
{
WebRequest webRequest = WebRequest.Create(uri);
webRequest.ContentType = "application/x-www-form-urlencoded"; // <- Should this be "application/json" ?
webRequest.Method = "POST";
byte[] bytes = Encoding.UTF8.GetBytes(parameters);
string byteString = Encoding.UTF8.GetString(bytes);
Stream os = null;
try
{ // Send the Post Data
webRequest.ContentLength = bytes.Length;
os = webRequest.GetRequestStream();
os.Write(bytes, 0, bytes.Length);
Console.WriteLine(String.Format(@"{0}", byteString)); // <- This matches the Json object
}
catch (WebException ex)
{ //Handle Error }
try
{ // Get the response
WebResponse webResponse = …Run Code Online (Sandbox Code Playgroud)