簡體   English   中英

如何在 C# 中使用 HTTP 請求發送 JSON GET 數據

[英]How to send JSON GET data with an HTTP request in C#

所以我在如何以 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 請求非常陌生,因此將不勝感激。

我想到了!

    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 數據(字符串格式)。 根據您使用的節拍,這對於數據可視化可能非常酷。

以下是我如何使用 POST 發送 Unity WWW請求:

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

並跳過我的方法的前兩行。

在這里,我們使用 IEnumerator 來yield return www:等待請求完成直到繼續。 要使用 IEnumerator 方法,您可以使用StartCoroutine(SendSomething());調用它StartCoroutine(SendSomething()); 這將異步運行。

暫無
暫無

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

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