【发布时间】:2017-01-27 15:03:40
【问题描述】:
所以我在如何以 C# 格式发送以下 JSON 数据时遇到了很多麻烦。我确切地知道如何在 cURL 中做到这一点,但我无法终生解决这个问题。这个请求对我正在做的事情至关重要,我真的需要完成它。这是 curl 语句:
curl <ip of server>/<index>/_search?pretty=true -d '
{
"query": {
"match_all": {}
},
"size": 1,
"sort": [{
"_timestamp": {
"order": "desc"
}
}]
}
如果它有帮助,我正在向 Elasticsearch 服务器发出请求,并且我正在获取 JSON 数据。这个 cURL 请求正好给了我我需要的东西。这是我现在拥有的 C# 代码,但我不确定如何将此 JSON 数据添加到 GET 请求中。这也将在 Unity 游戏引擎中运行。
// Create a request for the URL.
request = WebRequest.Create(elk_url);
// If required by the server, set the credentials.
request.Credentials = CredentialCache.DefaultCredentials;
// Get the response.
response = (HttpWebResponse)request.GetResponse();
// Display the status.
Debug.Log(response.StatusDescription);
// Get the stream containing content returned by the server.
dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
// Display the content.
Debug.Log(responseFromServer);
// Cleanup the streams and the response.
reader.Close();
dataStream.Close();
response.Close();
以上内容仅来自文档页面,我对代码中的 HTTP 请求非常陌生,因此将不胜感激。
【问题讨论】:
标签: c# json elasticsearch unity3d httpwebrequest