这是我在应用端编写的代码,正在使用Xamarin
开发
HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(ApiUrl);
objRequest.Method = "POST";
objRequest.ContentType = "application/x-www-form-urlencoded";//"application/json;charset=utf-8";
objRequest.Headers.Add("Authentication", "web60134");
UTF8Encoding utf8Encoding = new UTF8Encoding();
byte[] arrRequest;
arrRequest = utf8Encoding.GetBytes(JsonReq);
objRequest.ContentLength = arrRequest.Length;
Stream requestStream = objRequest.GetRequestStream();
requestStream.Write(arrRequest, 0, arrRequest.Length);
requestStream.Close();
HttpWebResponse res = (HttpWebResponse)objRequest.GetResponse();
StreamReader streamReader = new StreamReader(res.GetResponseStream());
string result = streamReader.ReadToEnd();
这是写在服务端的PHP代码
$recvd = $_POST['TH_ET_LoginProcess'];
$recvdArr = json_decode($recvd);
我尝试使用 file_get_contents("php://input")
但两种方法都返回 null 或什么都不返回。
在这种情况下,使用 PHP 在 Web 服务器上读取 JSON 的最佳方法是什么?
最佳答案
最佳 是一个相当主观的术语。
但是我成功使用了什么RestSharp在 Xamarin Android 解决方案中。
我也强烈推荐PostMan和 Fiddler用于对您的服务进行故障排除。
关于您的代码:
您应该处理那些一次性的对象,最好使用“using”语句。这包括 HttpWebResponse 对象以及 Stream 对象。
关于c# - 无法从使用 C# 开发的 Android 应用程序接收 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33543624/