首页 文章

从winform POST到REST API返回空

提问于
浏览
0

我有一个使用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;
                    }
                }

1 回答

  • 0

    尝试将密钥放在URL中(由文档概述):

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

相关问题