簡體   English   中英

如何從服務器到客戶端檢索響應作為URL

[英]How to retrieve response as URL from server to client

我有PHP Server,我將在其中發送來自客戶端的發送請求,並且我的請求成功進行,但是我希望來自服務器的響應應該是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 }

================================================== ==========================

我已經在服務器代碼中嘗試過:

   $sql =  "CALL FetchImage()";

    $res = mysqli_query($conn,$sql); 
    if($res == TRUE)
    {
        if (mysqli_num_rows($res) > 0) 
        {
            // output data of each row
            while($row = mysqli_fetch_assoc($res)) {
                echo json_encode($row);
            }
        }
    }

這是我的客戶端代碼:

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);

我從服務器得到的答復是:

================================================== ==========================

{\"image\":\"http:\\/\\/localhost\\/example\\/images\\/pic1.png\"}

================================================== ==========================

我想要來自服務器的准確或絕對URL。 我如何得到它,請幫助...

我嘗試了這個:

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

但是我正在將此異常添加到stringContent

================================================== ============================= {Newtonsoft.Json.JsonReaderException:讀取完JSON內容后遇到的其他文本:{。 路徑'',第3行,位置在Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(Newtonsoft.Json.Json.JType.Reader System(Newtonsoft.Json.Json.JType.Reader,在Newtonsoft.Json.JsonSerializer.DeserializeInternal(Newtonsoft.Json.Json.JsonReader reader,System.Type objectType)中的<12891e825fce44a581e5bbbb579c1d49>:0中的objectType,System.Boolean checkAdditionalContent [0x000db] [0x00054]在<12891e5dbb49>中的New0:0c00tontonJJ:0c .JsonSerializer.Deserialize(Newtonsoft.Json.JsonReader reader,System.Type objectType)[0x00000]在Newtonsoft.Json.Json.Convert.DeserializeObject(System.String值,System.Type類型,NewtonSonic.Jizer。設置)<12891e825fce44a581e5bbbb579c1d49>:0中的[0x0002d]在Newtonsoft.Json.JsonConvert.DeserializeObject [T](System.String值,Newtonsoft.Json.JsonSerializerSettings設置)在[12891e825fc中[0x00000] <12891e825fce44a581e5bbbb579c1d49>:0中的Newtonsoft.Json.JsonConvert.DeserializeObject [T](System.String value)[0x00000]中的e44a581e5bbbb579c1d49>:0

在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);

現在基於服務器代碼

// output data of each row
while($row = mysqli_fetch_assoc($res)) {
    echo json_encode($row);
}

如果集合中有多行,則有可能返回格式錯誤的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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM