locked
Login to website by POST methods using RestSharp. RRS feed

  • Question

  • User973383631 posted

    Hello, i have problem with this methods. I write function that parse value like login and password to website kosmiczni.pl

            public bool Login() {
                try {
                    Form.Log($"Loguję się na konto {this.Nick}");
                    openPage();
                    RestRequest loginRequest = new RestRequest("/log.php", Method.POST);
                    loginRequest.AddHeader("Host", "kosmiczni.pl");
                    loginRequest.AddHeader("Accept", "application/json, text/javascript, */*; q=0.01");
                    loginRequest.AddHeader("Origin", "https://kosmiczni.pl");
                    loginRequest.AddHeader("X-Requested-With", "XMLHttpRequest");
                    loginRequest.AddHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
                    loginRequest.AddHeader("Referer", "https://kosmiczni.pl/");
                    loginRequest.AddHeader("Accept-Encoding", "gzip, deflate, br");
                    loginRequest.AddHeader("Accept-Language", "pl,en-US;q=0.7,en;q=0.3");
                    loginRequest.AddParameter("login", this.Nick);
                    loginRequest.AddParameter("pass", this.Password);
                    loginRequest.AddParameter("serw", this.Server);
                    loginRequest.AddParameter("cookie", 0);
                    loginRequest.AddParameter("memory", "off");
    				loginRequest.AddParameter("cg", 1);
    
                    string response = this.Client.Execute(loginRequest).Content;
                    var json = JsonConvert.DeserializeObject<dynamic>(response);
                    if (json.done == 1) {
                        openGameFirst();
                        openGameSecond();
                        openTop();
                        ScrapePlayers();
                        return true;
                    }
                    return false;
                }
                catch {
                    return false;
                }
    
            }

    But.. no working
    I have this error unexpected character encountered while parsing value: e. path '', line 0, position 0.
    Can someone help me with this?

    Wednesday, June 21, 2017 4:59 AM

Answers

  • User1967761114 posted

    Hi  whiteblXK,

    See the following picture, this is the response which I received from the server after run your application.

    This response is an invalid JSON string format.

    There has some errors with the response of Web API server, I think you’d better modify the server response.

    However ,in your case, if you want run succeeds, you could refer to the following code.

    string response = this.Client.Execute(loginRequest).Content;
    //\u0001l\0\u0004{\"done\":1,\"url\":\".\\/game10\"}\u0003
    if (response.StartsWith("\u0001"))
    {
        response = response.Replace("\u0001l\0\u0004", "").Replace("\u0003", "");
    }
    var json = JsonConvert.DeserializeObject<dynamic>(response);

     

    If you have any other questions, please feel free to contact me any time.

    Best Regards

    Even

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 23, 2017 7:35 AM

All replies

  • User1967761114 posted

    Hi whiteblXK,

    According to your description, I’m unsure which error occurred, could you show more details about this exception.

    According to my experience, the exception seems occurred by the following line:

    JsonConvert.DeserializeObject<dynamic>(response);

    Make sure the response content is a correct JSON format, or you could show the response content in this thread, we will help you to solve this issue.

     

    If you have any other questions, please feel free to contact me any time.

    Best Regards

    Even

    Thursday, June 22, 2017 5:52 AM
  • User973383631 posted

    Hello, thanks for reply. You have right about 

    JsonConvert.DeserializeObject<dynamic>(response);

    when I point it , Visual Studio moves me to this line.

    So, my JSON response content is:

    {"done":1,"url":".\/game"}

    Thursday, June 22, 2017 6:27 AM
  • User1967761114 posted

    Hi whiteblXK,

    The JSON string format seems like OK, could you return any special characters?

    I suggest you could try to just return {“done”:1} to test whether it is works fine.

     

    If you have any other questions, please feel free to contact me any time.

    Best Regards

    Even

    Thursday, June 22, 2017 7:14 AM
  • User973383631 posted

    Nothing change when i only return done :1, again have this error

    Thursday, June 22, 2017 7:45 AM
  • User1967761114 posted

    Hi whiteblXK,

    According to your description, I suggest you could  try to check whether the request is succeed.

    If yes, you could provide the JSON string in this thread.

    If no, you could provide the exception detail info in thread.

    See the following code:

    var result = this.Client.Execute(loginRequest);
    if (result.StatusCode == HttpStatusCode.OK)
    {
        var json = JsonConvert.DeserializeObject<dynamic>(result.Content);
        if (json.done == 1) {
            //...
            //statements...
            //...
        }
    }
    else
    {
        var exception = result.ErrorException;
        //your statements to process error ...
    }

     

    If you have any other questions, please feel free to contact me any time.

    Best Regards

    Even

    Thursday, June 22, 2017 9:06 AM
  • User973383631 posted

    Tested, I print something if http status code is OK and thats works fine. Problem still with 

    var json = JsonConvert.DeserializeObject<dynamic>(result.Content);

    and error 

    Newtonsoft.Json.dll („Unexpected character encountered while parsing value: . Path '', line 0, position 0.”).

    Thursday, June 22, 2017 9:16 AM
  • User1967761114 posted

    Hi whiteblXK,

    This issue must be that had some format error with the JSON string, you'd better provide the result.Content to me, I want to check it.

     

    Best Regards

    Even

    Thursday, June 22, 2017 9:28 AM
  • User973383631 posted

    I don't understand you, can you be more specific? Do you want to look into my project files? If yes, it's no problem.

    Thursday, June 22, 2017 9:40 AM
  • User1967761114 posted

    Hi whitebIXK,

    I want to see the JSON string which responded by the server, I consider that must be had some error on the format.

     

    If you have any other questions, please feel free to contact me any time.

    Best Regards

    Even

    Thursday, June 22, 2017 10:04 AM
  • User973383631 posted

    Again I dont understand what you mean by JSON string, can you show me example of what you need? I'm beginner...

    Thursday, June 22, 2017 10:19 AM
  • User1967761114 posted

    Hi whitebIXK :),

    string response = this.Client.Execute(loginRequest).Content;

    I want to see the value of  response.

     

    If you have any other questions, please feel free to contact me any time.

    Best Regards

    Even

    Thursday, June 22, 2017 10:22 AM
  • User973383631 posted

    Hmm, I try print with System.Diagnostics.Debug.WriteLine(response); and it shows me:

    Thursday, June 22, 2017 10:43 AM
  • User1967761114 posted

    Hi whiteblXK,

    I think you'd better send the project to me...

    wxmayifei@hotmail.com

     

    If you have any other questions, please feel free to contact me any time.

    Best Regards

    Even

    Thursday, June 22, 2017 10:59 AM
  • User1967761114 posted

    Hi  whiteblXK,

    See the following picture, this is the response which I received from the server after run your application.

    This response is an invalid JSON string format.

    There has some errors with the response of Web API server, I think you’d better modify the server response.

    However ,in your case, if you want run succeeds, you could refer to the following code.

    string response = this.Client.Execute(loginRequest).Content;
    //\u0001l\0\u0004{\"done\":1,\"url\":\".\\/game10\"}\u0003
    if (response.StartsWith("\u0001"))
    {
        response = response.Replace("\u0001l\0\u0004", "").Replace("\u0003", "");
    }
    var json = JsonConvert.DeserializeObject<dynamic>(response);

     

    If you have any other questions, please feel free to contact me any time.

    Best Regards

    Even

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 23, 2017 7:35 AM