http get请求中的pass参数不起作用

时间:2018-04-07 14:21:32

标签: php xamarin

我有以下代码从服务器获取数据,我想传递带有请求的参数,如何在客户端和服务器端以正确的方式执行此操作

 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'")

2 个答案:

答案 0 :(得分:0)

您没有正确使用String.Format。您必须指定占位符令牌才能包含参数。您也没有正确格式化您的网址

string.Format("http://reksha.com/Coordinates/driversLocations.php?type={0}", type);

答案 1 :(得分:0)

微软有一篇关于如何使用HttpClient的文章。在他们的示例中,他们使用的是共享客户端对象。通常,您可以将它包装在using语句中。

https://docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/calling-a-web-api-from-a-net-client