【问题标题】:How to send JSON GET data with an HTTP request in C#如何在 C# 中使用 HTTP 请求发送 JSON GET 数据
【发布时间】: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


    【解决方案1】:

    我想通了!

        WebRequest request = WebRequest.Create(elk_url);
    
        request.ContentType = "application/json";
        request.Method = "POST";
        byte[] buffer = Encoding.GetEncoding("UTF-8").GetBytes(queryString);
        string result = System.Convert.ToBase64String(buffer);
        Stream reqstr = request.GetRequestStream();
        reqstr.Write(buffer, 0, buffer.Length);
        reqstr.Close();
    
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        Debug.Log(response.StatusDescription);
        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();
    

    查询字符串是原始帖子中的 JSON 数据。对于那些想了解 ELK 堆栈的人,这将为您提供来自最近事件的 JSON 数据(以字符串格式)。根据您使用的节拍,这对于数据可视化可能非常酷。

    【讨论】:

    • 如何将“结果”从 queryString 字节缓冲区传递给请求对象?
    【解决方案2】:

    以下是我如何通过 POST 发送 Unity WWW-request:

        public IEnumerator SendSomething()
        {
            WWWForm wwwForm = new WWWForm();
            wwwForm.AddField("Parameter_Name", jsonString);
    
            WWW www = new WWW(url, wwwForm);
            yield return www;
    
            if (www.error == null)
            {
                 Debug.Log("Everything worked!");
            }
            else
            {
                 Debug.Log("Something went wrong: " + www.error);
            }
        }
    

    如果你不提供 postData 参数(我的 wwwForm),WWW 类默认为 GET,所以如果你想使用 GET,你可以只为 WWW 类提供:

    WWW www = new WWW(url + "?" + jsonString);
    

    并跳过我方法的前 2 行。

    在这里,我们将 IEnumerator 用于yield return www:,它会等待请求完成,直到继续。要使用 IEnumerator 方法,请使用 StartCoroutine(SendSomething()); 调用它,这将异步运行。

    【讨论】:

    • 运行时收到“400 Bad Request”
    猜你喜欢
    • 1970-01-01
    • 2015-03-15
    • 1970-01-01
    • 1970-01-01
    • 2016-08-04
    • 2011-05-26
    • 1970-01-01
    • 2011-05-26
    • 2017-04-21
    相关资源
    最近更新 更多