Как запустить Unirest на с#

340
08 февраля 2018, 14:08

Пытаюсь подключить либу Unirest я понимаю что здесь все написано http://unirest.io/net но не могу понять что надо вписать и как создать MyClass

сам код который тестирую

HttpResponse<MyClass> jsonResponse = Unirest.post("http://httpbin.org/post")
.header("accept", "application/json")
.field("parameter", "value")
.field("foo", "bar")
.asJson<MyClass>();

На данный момент ошибки лезут

The type or namespace name 'MyClass' could not be found (are you missing a using directive or an assembly reference?)

Cannot implicitly convert type 'unirest_net.request.HttpRequest' to 'System.Threading.Tasks.Task>

Answer 1

Итак. В ходе эксперимента

var gif_id = "feqkVgjJpYtjy";
unirest_net.http.HttpResponse<string> jsonResponse = unirest_net.http.Unirest.get("https://giphy.p.mashape.com/v1/gifs/"+gif_id+"?api_key=dc6za123")
.header("X-Mashape-Key", "SNERVLqh5Omshu9HNTnca3coHgwop10IrHw123")
.header("Accept", "application/json")
.asJson<string>();
return  jsonResponse.Body;

Мы получили некий json. Я обычно в таких случаях, чтоб не особо парится иду на онлайновые сервисы например на https://jsonutils.com/. Вставляем туда этот самый json и получаем такую вот партянку классов (сложный json достаточно, ничего не поделаешь - в принципе не всегда имеет смысл настолько полно его покрывать, и все это можно порезать до минимально необходимого вам уровня - и все будет прекрасно десериализовываться, но мне не известно что именно вам из всего этого надо, поэтому вот:

public class User
{
    public string avatar_url { get; set; }
    public string banner_url { get; set; }
    public string profile_url { get; set; }
    public string username { get; set; }
    public string display_name { get; set; }
    public string twitter { get; set; }
}
public class FixedHeightStill
{
    public string url { get; set; }
    public string width { get; set; }
    public string height { get; set; }
    public string size { get; set; }
}
public class OriginalStill
{
    public string url { get; set; }
    public string width { get; set; }
    public string height { get; set; }
    public string size { get; set; }
}
public class FixedWidth
{
    public string url { get; set; }
    public string width { get; set; }
    public string height { get; set; }
    public string size { get; set; }
    public string mp4 { get; set; }
    public string mp4_size { get; set; }
    public string webp { get; set; }
    public string webp_size { get; set; }
}
public class FixedHeightSmallStill
{
    public string url { get; set; }
    public string width { get; set; }
    public string height { get; set; }
    public string size { get; set; }
}
public class FixedHeightDownsampled
{
    public string url { get; set; }
    public string width { get; set; }
    public string height { get; set; }
    public string size { get; set; }
    public string webp { get; set; }
    public string webp_size { get; set; }
}
public class Preview
{
    public string width { get; set; }
    public string height { get; set; }
    public string mp4 { get; set; }
    public string mp4_size { get; set; }
}
public class FixedHeightSmall
{
    public string url { get; set; }
    public string width { get; set; }
    public string height { get; set; }
    public string size { get; set; }
    public string mp4 { get; set; }
    public string mp4_size { get; set; }
    public string webp { get; set; }
    public string webp_size { get; set; }
}
public class DownsizedStill
{
    public string url { get; set; }
    public string width { get; set; }
    public string height { get; set; }
    public string size { get; set; }
}
public class Downsized
{
    public string url { get; set; }
    public string width { get; set; }
    public string height { get; set; }
    public string size { get; set; }
}
public class DownsizedLarge
{
    public string url { get; set; }
    public string width { get; set; }
    public string height { get; set; }
    public string size { get; set; }
}
public class FixedWidthSmallStill
{
    public string url { get; set; }
    public string width { get; set; }
    public string height { get; set; }
    public string size { get; set; }
}
public class PreviewWebp
{
    public string url { get; set; }
    public string width { get; set; }
    public string height { get; set; }
    public string size { get; set; }
}
public class FixedWidthStill
{
    public string url { get; set; }
    public string width { get; set; }
    public string height { get; set; }
    public string size { get; set; }
}
public class FixedWidthSmall
{
    public string url { get; set; }
    public string width { get; set; }
    public string height { get; set; }
    public string size { get; set; }
    public string mp4 { get; set; }
    public string mp4_size { get; set; }
    public string webp { get; set; }
    public string webp_size { get; set; }
}
public class DownsizedSmall
{
    public string width { get; set; }
    public string height { get; set; }
    public string mp4 { get; set; }
    public string mp4_size { get; set; }
}
public class FixedWidthDownsampled
{
    public string url { get; set; }
    public string width { get; set; }
    public string height { get; set; }
    public string size { get; set; }
    public string webp { get; set; }
    public string webp_size { get; set; }
}
public class DownsizedMedium
{
    public string url { get; set; }
    public string width { get; set; }
    public string height { get; set; }
    public string size { get; set; }
}
public class Original
{
    public string url { get; set; }
    public string width { get; set; }
    public string height { get; set; }
    public string size { get; set; }
    public string frames { get; set; }
    public string mp4 { get; set; }
    public string mp4_size { get; set; }
    public string webp { get; set; }
    public string webp_size { get; set; }
    public string hash { get; set; }
}
public class FixedHeight
{
    public string url { get; set; }
    public string width { get; set; }
    public string height { get; set; }
    public string size { get; set; }
    public string mp4 { get; set; }
    public string mp4_size { get; set; }
    public string webp { get; set; }
    public string webp_size { get; set; }
}
public class Looping
{
    public string mp4 { get; set; }
    public string mp4_size { get; set; }
}
public class OriginalMp4
{
    public string width { get; set; }
    public string height { get; set; }
    public string mp4 { get; set; }
    public string mp4_size { get; set; }
}
public class PreviewGif
{
    public string url { get; set; }
    public string width { get; set; }
    public string height { get; set; }
    public string size { get; set; }
}
public class 480wStill
{
    public string url { get; set; }
    public string width { get; set; }
    public string height { get; set; }
}
public class Images
{
    public FixedHeightStill fixed_height_still { get; set; }
    public OriginalStill original_still { get; set; }
    public FixedWidth fixed_width { get; set; }
    public FixedHeightSmallStill fixed_height_small_still { get; set; }
    public FixedHeightDownsampled fixed_height_downsampled { get; set; }
    public Preview preview { get; set; }
    public FixedHeightSmall fixed_height_small { get; set; }
    public DownsizedStill downsized_still { get; set; }
    public Downsized downsized { get; set; }
    public DownsizedLarge downsized_large { get; set; }
    public FixedWidthSmallStill fixed_width_small_still { get; set; }
    public PreviewWebp preview_webp { get; set; }
    public FixedWidthStill fixed_width_still { get; set; }
    public FixedWidthSmall fixed_width_small { get; set; }
    public DownsizedSmall downsized_small { get; set; }
    public FixedWidthDownsampled fixed_width_downsampled { get; set; }
    public DownsizedMedium downsized_medium { get; set; }
    public Original original { get; set; }
    public FixedHeight fixed_height { get; set; }
    public Looping looping { get; set; }
    public OriginalMp4 original_mp4 { get; set; }
    public PreviewGif preview_gif { get; set; }
    public 480wStill 480w_still { get; set; }
}
public class Data
{
    public string type { get; set; }
    public string id { get; set; }
    public string slug { get; set; }
    public string url { get; set; }
    public string bitly_gif_url { get; set; }
    public string bitly_url { get; set; }
    public string embed_url { get; set; }
    public string username { get; set; }
    public string source { get; set; }
    public string rating { get; set; }
    public string content_url { get; set; }
    public string source_tld { get; set; }
    public string source_post_url { get; set; }
    public int is_indexable { get; set; }
    public int is_sticker { get; set; }
    public string import_datetime { get; set; }
    public string trending_datetime { get; set; }
    public User user { get; set; }
    public Images images { get; set; }
    public string title { get; set; }
}
public class Meta
{
    public int status { get; set; }
    public string msg { get; set; }
    public string response_id { get; set; }
}
public class MyClass
{
    public Data data { get; set; }
    public Meta meta { get; set; }
}

Собственно как-то так мы получили требуемый MyClass.

Answer 2

Решение было найдено! Там где <MyClass> поменял на <string>.

var gif_id = "feqkVgjJpYtjy";
unirest_net.http.HttpResponse<string> jsonResponse = unirest_net.http.Unirest.get("https://giphy.p.mashape.com/v1/gifs/"+gif_id+"?api_key=dc6za123")
.header("X-Mashape-Key", "SNERVLqh5Omshu9HNTnca3coHgwop10IrHw123")
.header("Accept", "application/json")
.asJson<string>();
return  jsonResponse.Body;
READ ALSO
Отчет по сети AdMob [Unity][Google play console]

Отчет по сети AdMob [Unity][Google play console]

Подскажите пожалуйста, почему (на скрине) запросов сети намного больше чем сопоставимых? С чем это может быть связано? Реклама показывается...

233
Как получить UserManager

Как получить UserManager

В Веб-Приложении значение UserManager получается через конструктор (не знаю, если честно, как он туда попадает)

231