C# 如何将响应作为URL从服务器检索到客户端

C# 如何将响应作为URL从服务器检索到客户端,c#,xamarin.forms,C#,Xamarin.forms,我有一个PHP服务器,我将从客户端发送请求,我的请求正在成功进行,但我希望从服务器得到响应,该响应应该是URL,当我试图从服务器检索响应(URL作为响应)时,它将成功进行,但我的StringContent对象出现异常。即: ============================================================================= {System.UriFormatException: Invalid URI: The format of the

我有一个PHP服务器,我将从客户端发送请求,我的请求正在成功进行,但我希望从服务器得到响应,该响应应该是URL,当我试图从服务器检索响应(URL作为响应)时,它将成功进行,但我的StringContent对象出现异常。即: =============================================================================

 {System.UriFormatException: Invalid URI: The format of the URI could not be determined.
  at System.Uri.CreateThis (System.String uri, System.Boolean dontEscape, System.UriKind uriKind) [0x0007b] in <ca7419b40e504a6dbe088f6fe95d09aa>:0 
  at System.Uri..ctor (System.String uriString, System.UriKind uriKind) [0x00014] in <ca7419b40e504a6dbe088f6fe95d09aa>:0 
  at MPTrain.view.ProductList.SendRequest () [0x001aa] in C:\nginx\www\repos\xformsexperimental\MPTrain\MPTrain\MPTrain\view\ProductList.xaml.cs:111 }
{\"image\":\"http:\\/\\/localhost\\/example\\/images\\/pic1.png\"}
这是我的客户端代码:

HttpClient client = new HttpClient();

client.BaseAddress = new Uri("http://192.168.1.011/repos/xformsexperimental/RestApiTrain/index.php");

Dictionary<object, object> keyValuePairs = new Dictionary<object, object>();
keyValuePairs.Add("eml", "Manisha");

var jsonData = JsonConvert.SerializeObject(keyValuePairs);
var content = new StringContent(jsonData, UnicodeEncoding.UTF8, "application/json");

var posttask = await client.PostAsync(client.BaseAddress.ToString(), content); //accessing response

var stringContent = await posttask.Content.ReadAsStringAsync();

// ResponseImage responseImage = JsonConvert.DeserializeObject<ResponseImage>(stringContent);

Uri uri = new Uri(stringContent,UriKind.Absolute);
=============================================================================

 {System.UriFormatException: Invalid URI: The format of the URI could not be determined.
  at System.Uri.CreateThis (System.String uri, System.Boolean dontEscape, System.UriKind uriKind) [0x0007b] in <ca7419b40e504a6dbe088f6fe95d09aa>:0 
  at System.Uri..ctor (System.String uriString, System.UriKind uriKind) [0x00014] in <ca7419b40e504a6dbe088f6fe95d09aa>:0 
  at MPTrain.view.ProductList.SendRequest () [0x001aa] in C:\nginx\www\repos\xformsexperimental\MPTrain\MPTrain\MPTrain\view\ProductList.xaml.cs:111 }
{\"image\":\"http:\\/\\/localhost\\/example\\/images\\/pic1.png\"}
我想从服务器上得到一个准确或绝对的URL。 我怎样才能得到它请帮助

我试过这个:

ResponseImage responseImage = JsonConvert.DeserializeObject<ResponseImage>(stringContent);
ResponseImage ResponseImage=JsonConvert.DeserializeObject(stringContent);
但我正在将此例外添加到stringContent上

================================================================================ {Newtonsoft.Json.JsonReaderException:在读完Json内容后遇到其他文本:{.Path',第3行,位置0。 位于:0中的Newtonsoft.Json.JsonTextReader.Read()[0x000c7] 在Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(Newtonsoft.Json.JsonReader,System.Type objectType,System.Boolean checkAdditionalContent)[0x000db]中:0 位于Newtonsoft.Json.JsonSerializer.DeserializeInternal(Newtonsoft.Json.JsonReader reader,System.Type objectType)[0x00054]中:0 在Newtonsoft.Json.JsonSerializer.Deserialize(Newtonsoft.Json.JsonReader reader,System.Type objectType)[0x00000]中:0 在Newtonsoft.Json.JsonConvert.DeserializeObject(System.String值,System.Type类型,Newtonsoft.Json.JsonSerializerSettings设置)[0x0002d]中:0 在Newtonsoft.Json.JsonConvert.DeserializeObject[T](System.String值,Newtonsoft.Json.JsonSerializerSettings设置)[0x00000]中:0 0中的Newtonsoft.Json.JsonConvert.DeserializeObject[T](System.String值)[0x00000]处


在C:\nginx\www\repos\xformsexperimental\MPTrain\MPTrain\MPTrain\view\ProductList.xaml.cs:112}中的MPTrain.view.ProductList.SendRequest()[0x001aa]处,响应似乎是JSON

看起来您试图使用强类型模型,因此基于所示的JSON,该模型应该如下所示

public class ResponseImage {
    public string image { get; set; }
}
以匹配预期数据

解析响应并提取所需信息

//...

ResponseImage responseImage = JsonConvert.DeserializeObject<ResponseImage>(stringContent);

Uri uri = new Uri(responseImage.image, UriKind.Absolute);
如果集合中有多行,则可能返回格式错误的JSON

我建议您填充一个适当的集合,然后将其作为JSON数组返回

//...

if (mysqli_num_rows($res) > 0)  {
    $result = array();
    // collect data of each row
    while($row = mysqli_fetch_assoc($res)) {
        $result[] = $row; //push data into array
    }
    echo json_encode($result);
}

//...
然后需要将客户端代码更新为预期的集合,而不是单个对象

ResponseImage[] responseImages = JsonConvert.DeserializeObject<ResponseImage[]>(stringContent);

Uri[] uris = responseImages.Select(x => new Uri(x.image, UriKind.Absolute)).ToArray();
ResponseImage[]responseImages=JsonConvert.DeserializeObject(stringContent);
Uri[]uris=responseImages.Select(x=>newURI(x.image,UriKind.Absolute)).ToArray();