簡體   English   中英

從Winform POST到REST API返回空

[英]POST to REST API from winform returns empty

我有一個使用hockeystreams.com API的項目。 我在使用他們的API從WinForm登錄時遇到麻煩。 無論輸入的用戶名/密碼組合錯誤還是​​輸入正確,它總是給我空白的答復。 我的代碼如下:

public static bool LogIn(string username, string password)
    {
        string URL = string.Format("{0}&username={1}&password={2}", Global.LOGIN_API, username, password);
        HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(URL);
        webRequest.ContentType = "application/json; charset=utf-8";
        webRequest.Accept = "application/json, text/javascript, */*";
        webRequest.Method = "POST";
        try
        {

            WebResponse response = webRequest.GetResponse();
            Stream stream = response.GetResponseStream();
            string json = "";

            using (StreamReader reader = new StreamReader(stream))
            {
                while (!reader.EndOfStream)
                {
                    json += reader.ReadLine();
                }
            }

            LogInJSON deserializedLogInData = JsonConvert.DeserializeObject<LogInJSON>(json);
            if (deserializedLogInData.Status == "Success")
            {
                Global.TOKEN = deserializedLogInData.Token;
                Global.USERNAME = deserializedLogInData.Username;
                Global.ISPREMIUM = deserializedLogInData.Membership == "REGULAR" ? false : true;
                Global.FAVORITETEAM = deserializedLogInData.Favteam;

                return true;
            }
            else
            {
                return false;
            }
        }

我嘗試將POST更改為PUT,但這會引發406錯誤(錯誤請求)。 我對JSON非常滿意,所以我不知道從這里去哪里。 任何幫助,不勝感激!

編輯:我在hockeystreams網站上發現了與我的問題類似的內容,但是正在使用apache httpclient。

    With buckinpgh's help I resolved my issue.

Just for anyone else out there who is wondering, the solution (if your using apache HttpClient) is to create a HttpPost request with the url: "https://api.hockeystreams.com/Login?"

then add two headers:
1) StringEntity with the value: "username=<username>&password=<password>&key=<apiKey>"
2) "content-type", "application/x-www-form-urlencoded"

after that you can just call HttpClient.execute with your request. Of course you'll have to parse the json result, but at least now you have a result.

我更改了代碼,以嘗試使用HttpWebRequest進行最佳操作:

public static bool LogIn(string username, string password)
    {
        string URL = Global.LOGIN_PATH;
        HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(URL);
        webRequest.ContentType = "application/x-www-form-urlencoded";
        webRequest.Accept = "application/json, text/javascript, */*";
        webRequest.Method = "POST";
        webRequest.Headers.Add("StringEntity", string.Format("username={0}&password={1}&key={2}", username, password, Global.APIKEY);
        try 
        {
            WebResponse response = webRequest.GetResponse();
            Stream stream = response.GetResponseStream();
            string json = "";

但這仍然是一個空白的回應。 我看不到有其他解決方法嗎?

編輯2:

最終得到了修復它的代碼。 我為此使用了RestSharp。

public static bool LogIn(string username, string password)
        {
            string URL = Global.LOGIN_PATH;
            try
            {
                var client = new RestClient("https://api.hockeystreams.com/");
                var request = new RestRequest("login?", Method.POST);
                request.AddHeader("content-type", "application/x-www-forms-urlencoded");
                request.AddParameter("username", username);
                request.AddParameter("password", password);
                request.AddParameter("key", Global.APIKEY);
                IRestResponse response = client.Execute(request);
                var json = response.Content;

                LogInJSON deserializedLogInData = JsonConvert.DeserializeObject<LogInJSON>(json);

                try
                {
                    if (deserializedLogInData.Status == "Success")
                    {
                        Global.TOKEN = deserializedLogInData.Token;
                        Global.USERNAME = deserializedLogInData.Username;
                        Global.ISPREMIUM = deserializedLogInData.Membership == "REGULAR" ? false : true;
                        Global.FAVORITETEAM = deserializedLogInData.Favteam;

                        return true;
                    }
                    else
                    {
                        Global.ERROR = deserializedLogInData.Msg;
                        return false;
                    }
                }

嘗試將密鑰放入URL中(文檔概述):

https://api.hockeystreams.com/Login?username=<username>&password=<password>&key=<api_key>

暫無
暫無

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

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