http request in c #

-1

Hi, I'm trying to make an application in C # related to a game. The problem I have is the following is that when I do the following always shows me the exception:

try
{
    string response = await _client.GetStringAsync(string.Format(Config.USER_REQUEST_FORMAT, Username));
    User user = User.fromJSON(response);
    UpdateInfo(user);
}
catch (Exception)
{
    MessageBox.Show("Error connecting to the habbo API, this is most likely because the username you specified does not exist", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

the link string is as follows:

public static string USER_REQUEST_FORMAT = "https://www.habbo.com/api/public/users?name={0}";

for example this is an example of the result that will return me link

but when I try to get this json I skip the exception

    
asked by Perl 25.10.2016 в 00:27
source

2 answers

2

It seems that this site requires that a value for UserAgent be set in the HTTP header. If you comment on line 4 of this code, the answer is a 463. If UserAgent is added, the answer is 200.

string page = "https://www.habbo.com/api/public/users?name=omar";
using (HttpClient client = new HttpClient())
{
    client.DefaultRequestHeaders.UserAgent.ParseAdd("Fiddler");
    using (HttpResponseMessage response = await client.GetAsync(page))
    using (HttpContent content = response.Content)
    {
        string result = await content.ReadAsStringAsync();
        if (result != null)
        {
            Console.WriteLine(result);
        }
    }
}
    
answered by 25.10.2016 / 05:28
source
0

My recommendation is that you use the Json.net library because it encapsulates a lot of the processes to make requests. Here is an example of what you want to do with that library:

static void ExampleHabbo(string username)
    {
        var client = new RestClient("https://www.habbo.com/api/public");
        var request = new RestRequest();
        request.Method = Method.GET;
        request.Resource = "users";
        request.RequestFormat = DataFormat.Json;
        request.AddHeader("Accept", "application/json");
        request.AddQueryParameter("name", username);

        IRestResponse response = client.Execute(request);

        if(response.StatusCode == System.Net.HttpStatusCode.OK)
        {
            var deserial = new JsonDeserializer();
            Dictionary<string, object> result = deserial.Deserialize<Dictionary<string, object>>(response);
        }
    }

You call that method you pass the name and it returns the json with its properties. In this image you can see the dictionary that I put in order to save the objects:)

    
answered by 28.10.2016 в 12:23