[英]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年世界杯第一名 我在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年世界杯第一名
it returns json string like below: 它返回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"
}
}
]
}
but when I send the URL in c#, see below: 但是当我使用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;
}
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 谁能在我的C#代码中告诉我原因以及如何解决此问题
You need to encode the url before making the call because it contains the non-Ascii characters. 在进行呼叫之前,您需要对网址进行编码,因为该网址包含非Ascii字符。
You can use the HttpUtility.UrlEncode
or you can do manually and refer the link 您可以使用
HttpUtility.UrlEncode
,也可以手动进行操作并引用链接
https://stackoverflow.com/a/8248262/6671466 https://stackoverflow.com/a/8248262/6671466
May be it will help 可能会有所帮助
You can use HttpWebRequest
library which is from System.Net 您可以使用来自System.Net的
HttpWebRequest
库
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. 如果您需要此返回字符串的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;
}
By using this you will get the response. 通过使用此方法,您将获得响应。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.