[英]Not able to receive json from Android app developed using C#
This is the code which I have written on the app side, which is being developed using Xamarin
这是我在应用程序端编写的代码,该代码正在使用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();
This is the code written on the server side which is PHP 这是写在服务器端的PHP代码
$recvd = $_POST['TH_ET_LoginProcess'];
$recvdArr = json_decode($recvd);
I tried using file_get_contents("php://input")
but both the methods are returning null or nothing. 我尝试使用file_get_contents("php://input")
但是两种方法都返回null或什么都不返回。
What is the best way to read JSON at web server using PHP in such scenario? 在这种情况下,使用PHP在Web服务器上读取JSON的最佳方法是什么?
Best is a quite subjective term. 最佳是一个相当主观的术语。
However what I am successfully using RestSharp in an Xamarin Android solution. 但是,我在Xamarin Android解决方案中成功使用RestSharp的方法。
I also highly recommend PostMan and Fiddler for troubleshooting your service. 我也强烈建议PostMan和Fiddler对您的服务进行故障排除。
Regarding your code: 关于您的代码:
You should be disposing those objects that are disposable, preferable with the 'using' statement. 您应该处理那些可抛弃的对象,最好使用“ using”语句。 This includes HttpWebResponse object is as well as the Stream ones. 这包括HttpWebResponse对象以及Stream对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.