簡體   English   中英

在C#中發送HTTP請求失敗

[英]send HTTP request failed in C#

我在Chrome瀏覽器中使用了以下URL,並且可以正常運行: https : //westus.api.cognitive.microsoft.com/luis/v2.0/apps/62bea90c-9b0c-487b-8416-1a4d94772f99?subscription-key=29317e2237fb4b43a91959cadee6f143&staging= true&verbose = true&timezoneOffset = 480&q =哪個國家獲得2010年世界杯第一名

它返回json字符串,如下所示:

{
  "query": "哪個國家獲得2010年世界杯第一名",
  "topScoringIntent": {
    "intent": "Query",
    "score": 0.9818858
  },
  "intents": [
    {
      "intent": "Query",
      "score": 0.9818858
    },
    {
      "intent": "None",
      "score": 0.01755463
    }
  ],
  "entities": [
    {
      "entity": "2010年",
      "type": "builtin.datetimeV2.daterange",
      "startIndex": 6,
      "endIndex": 10,
      "resolution": {
        "values": [
          {
            "timex": "2010",
            "type": "daterange",
            "start": "2010-01-01",
            "end": "2011-01-01"
          }
        ]
      }
    },
    {
      "entity": "2010",
      "type": "builtin.number",
      "startIndex": 6,
      "endIndex": 9,
      "resolution": {
        "value": "2010"
      }
    },
    {
      "entity": "一",
      "type": "builtin.number",
      "startIndex": 15,
      "endIndex": 15,
      "resolution": {
        "value": "1"
      }
    }
  ]
}

但是當我使用c#發送URL時,請參見以下內容:

    private void button1_Click(object sender, EventArgs e)
    {
        string uri = txtURL.Text;
        if (uri == null)
        {
            uri = @"https://westus.api.cognitive.microsoft.com/luis/v2.0/apps/62bea90c-9b0c-487b-8416-1a4d94772f99?subscription-key=29317e2237fb4b43a91959cadee6f143&staging=true&verbose=true&timezoneOffset=480&q=哪個國家獲得2010年世界杯第一名";
        }
        var json_contents = new WebClient().DownloadString(uri); //exception thrown from this line
        txtJson.Text = json_contents;
    }

我收到一條錯誤消息:

'The path is not of a legal form.'

誰能在我的C#代碼中告訴我原因以及如何解決此問題

在進行呼叫之前,您需要對網址進行編碼,因為該網址包含非Ascii字符。

您可以使用HttpUtility.UrlEncode ,也可以手動進行操作並引用鏈接

https://stackoverflow.com/a/8248262/6671466

可能會有所幫助

您可以使用來自System.Net的HttpWebRequest

如果您需要此返回字符串的json格式,則可以處理此代碼。通過json轉換庫,您可以將數據字符串轉換為json。

private void button1_Click(object sender, EventArgs e)
    {
        string json = string.Empty;
        string url = @"https://westus.api.cognitive.microsoft.com/luis/v2.0/apps/62bea90c-9b0c-487b-8416-1a4d94772f99?subscription-key=29317e2237fb4b43a91959cadee6f143&staging=true&verbose=true&timezoneOffset=480&q=哪個國家獲得2010年世界杯第一名";

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.AutomaticDecompression = DecompressionMethods.GZip;

        using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
        using (Stream stream = response.GetResponseStream())
        using (StreamReader reader = new StreamReader(stream))
        {
            json = reader.ReadToEnd();
        }

        richTextBox1.Text = json;
    }

通過使用此方法,您將獲得響應。

暫無
暫無

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

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