Php http get请求中的pass参数无效
我有下面的代码从服务器获取数据,我想在请求中传递参数,如何在客户端和服务器端以正确的方式传递参数Php http get请求中的pass参数无效,php,xamarin,Php,Xamarin,我有下面的代码从服务器获取数据,我想在请求中传递参数,如何在客户端和服务器端以正确的方式传递参数 string type = Intent.GetStringExtra("cartypeselect"); var request = HttpWebRequest.Create(string.Format(@"http://reksha.com/Coordinates/driversLocations.php", type)); request.ContentT
string type = Intent.GetStringExtra("cartypeselect");
var request = HttpWebRequest.Create(string.Format(@"http://reksha.com/Coordinates/driversLocations.php", type));
request.ContentType = "application/json; charset=utf-8";
request.Method = "GET";
var content = "";
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
if (response.StatusCode != HttpStatusCode.OK)
Console.Out.WriteLine("Error fetching data. Server returned status code: {0}", response.StatusCode);
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
content = reader.ReadToEnd();
if (string.IsNullOrWhiteSpace(content))
{
Console.Out.WriteLine("Response contained empty body...");
}
else
{
Console.Out.WriteLine("Response Body: \r\n {0}", content);
}
Assert.NotNull(content);
}
PHP代码
$cartype= $_GET['type'];
$result = mysqli_query($con,"select * from drivers where status='online' and latitude is not null and longitude is not null and car_type='$cartype'")
您没有正确使用String.Format。必须指定占位符标记才能包含参数。您也没有正确设置url的格式
string.Format("http://reksha.com/Coordinates/driversLocations.php?type={0}", type);
微软有一篇关于如何使用HttpClient的好文章。在他们的示例中,他们使用的是共享客户端对象。不过,通常情况下,您会将其包装在using语句中
Quick code review comment:使用response.IsSuccessStatusCode而不是检查OK。这仅适用于GET。您应该对值进行UrlEncode或UrlPathEncode编码,并使用字符串插值,而不是string.Format。