user2575502
user2575502

Reputation: 703

send HTTP request failed in C#

I use following URL in my chrome and it works fine: 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年世界杯第一名

it returns json string like below:

{
  "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"
      }
    }
  ]
}

but when I send the URL in c#, see below:

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

I got an error message:

'The path is not of a legal form.'

can anyone tell my the reason and how to fix this problem in my C# code

Upvotes: 1

Views: 1658

Answers (2)

Ashiqur Rahman Emran
Ashiqur Rahman Emran

Reputation: 207

You can use HttpWebRequest library which is from System.Net

which can handle this code if you need json format of this returned string there is json converting library by that you can convert you data string to 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;
    }

By using this you will get the response.

Upvotes: 3

Praveen Kumar
Praveen Kumar

Reputation: 2014

You need to encode the url before making the call because it contains the non-Ascii characters.

You can use the HttpUtility.UrlEncode or you can do manually and refer the link

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

May be it will help

Upvotes: 5

Related Questions