簡體   English   中英

從 API 獲取 json 對象,使用 C# 修改並發布它

[英]Get json object from API, modify & post it using C#

下面是我的代碼:

static void Main(string[] args)
{    
    SetBase();
    //Console.ReadLine();
}

public static async void SetBase()
{
    var baseAddress = new Uri("https://google.com/");
    var httpClient = new HttpClient { BaseAddress = baseAddress };
    httpClient.DefaultRequestHeaders.TryAddWithoutValidation("accept", "application/json");
    var content = new StringContent("{ \"postUserLogin\":{ \"login\":\"username\", \"password\":\"password\", \"remember\":1,\"verify_level\":0 }}", System.Text.Encoding.Default, "application/json");
    try
    {
        var response = await httpClient.PostAsync("/account/login", content);

        using (var newresponse = await httpClient.GetAsync(viewURL + viewurlParameters))
        {
            if (newresponse.IsSuccessStatusCode)
            {                
                string responseData = await newresponse.Content.ReadAsStringAsync();
                JObject json = JObject.Parse(responseData);

                //Get input from User
                string input = Console.ReadLine();
                //Make changes to json object according to input                
                //And Post Json object
            }
        }
    }
    catch (Exception ex)
    {

    }
}

之后的所有代碼: var response = await httpClient.PostAsync("/account/login", content); 不會被執行。

但是,如果我取消注釋MainConsole.Readline()行,其余步驟將被執行。 請幫忙。

是的,不要把它們放在一個循環中。 請添加將來不起作用的代碼,以避免您的問題被刪除。

按順序做:

  1. 使用登錄憑據調用 API 以獲取令牌字符串
  2. 使用您的數據請求和令牌點擊 API
  3. 如果要求,請注銷 API,因為接下來的步驟可能需要很長時間
  4. 進行用戶交互
  5. 將新項目添加到總列表
  6. 如果有新項目再次執行步驟1
  7. 使用數據上傳和您的新令牌點擊 API 端點。

暫無
暫無

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

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