WebResponse类放缓 [英] WebResponse slow

查看:111
本文介绍了WebResponse类放缓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用API​​来获取有关电影的信息,
即时期运用 API的它actualy工作。

的唯一一件事是方式慢,我wonderd如果有一个更快的方式做到这一点?我必须说,这是相当新的给我。这里是索姆code。

 公共字符串连接()
    {
        WebRequest的请求= WebRequest.Create(this.url);
        request.ContentType =应用/ JSON的;字符集= UTF-8;        //这就减慢我失望
        WebResponse的响应=(HttpWebResponse)request.GetResponse();        使用(StreamReader的SR =新的StreamReader(response.GetResponseStream()))
        {
            jsonString = sr.ReadToEnd();
        }
        返回jsonString;
    }    公共静态字符串GetFilmInfo(字符串Titel的)
    {
        MakeCon checkVat =新MakeCon(http://www.imdbapi.com/?i=&t=red+影片名称+/);
        JsonSerializer串行=新JsonSerializer();
        字符串jsonString = checkVat.Connect();
        返回JsonConvert.DeserializeObject<串GT;(jsonString);
    }


解决方案

你能做的最好是同时放跑多个请求,使用WebRequest.GetResponse通话,即WebRequest.BeginGetResponse / EndGetResponse的异步版本。下面是一个简单的例子:

 静态无效的主要(字串[] args)
    {
        RequestByTitle(碟中谍);
        RequestByTitle(碟中谍2);
        RequestByTitle(碟中谍3);
        RequestByTitle(肖申克的救赎);        到Console.ReadLine();
    }    私人常量字符串IMDBApiUrlFormatByTitle =
        http://www.imdbapi.com/?t={0};    私有静态无效RequestByTitle(字符串名称)
    {
        字符串URL =的String.Format(IMDBApiUrlFormatByTitle,职称);
        makeRequest的(URL);
    }    私有静态无效makeRequest的(字符串URL)
    {
        HttpWebRequest的REQ =(HttpWebRequest的)HttpWebRequest.Create(URL);
        req.ServicePoint.ConnectionLimit = 10;        req.BeginGetResponse(GetResponseCallback,REQ);
    }    私有静态无效GetResponseCallback(IAsyncResult的AR)
    {
        HttpWebRequest的REQ = ar.AsyncState为HttpWebRequest的;
        字符串结果;
        使用(WebResponse类RESP = req.EndGetResponse(AR))
        {
            使用(StreamReader的读者=新的StreamReader(
                resp.GetResponseStream())
                )
            {
                结果= reader.ReadToEnd();
            }
        }        Console.WriteLine(结果);
    }

请注意该行:

  req.ServicePoint.ConnectionLimit = 10;

它可以让你做2个以上的并发请求相同的服务端点(请参阅this帖子有详细介绍)。你应该仔细挑选的数量,以免违反使用期限(IMDB的API服务,如果有的话)。

I'm trying to use an api to get information about movies, i'm useing this api an it actualy works.

The only thing is that is way to slow and i wonderd if there is an faster way to do it? I must say, this is rather new for me. Here is somme code.

    public string Connect()
    {
        WebRequest request = WebRequest.Create(this.url);
        request.ContentType = "application/json; charset=utf-8";

        //this is slowing me down
        WebResponse response = (HttpWebResponse)request.GetResponse();

        using (StreamReader sr = new StreamReader(response.GetResponseStream()))
        {
            jsonString = sr.ReadToEnd();
        }
        return jsonString;
    }

    public static string GetFilmInfo(string titel)
    {
        MakeCon checkVat = new MakeCon("http://www.imdbapi.com/?i=&t=red" + titel + "/");
        JsonSerializer serializer = new JsonSerializer();
        string jsonString = checkVat.Connect();
        return JsonConvert.DeserializeObject<string>(jsonString);
    }

解决方案

The best you can do is to run multiple requests simultaniously, using the asynchronous version of WebRequest.GetResponse call, namely WebRequest.BeginGetResponse/EndGetResponse. Here is a simple example:

    static void Main(string[] args)
    {
        RequestByTitle("Mission Impossible");
        RequestByTitle("Mission Impossible 2");
        RequestByTitle("Mission Impossible 3");
        RequestByTitle("The Shawshank Redemption");

        Console.ReadLine();
    }

    private const String IMDBApiUrlFormatByTitle =
        "http://www.imdbapi.com/?t={0}";

    private static void RequestByTitle(String title)
    {
        String url = String.Format(IMDBApiUrlFormatByTitle, title);
        MakeRequest(url);
    }

    private static void MakeRequest(String url)
    {
        HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
        req.ServicePoint.ConnectionLimit = 10;

        req.BeginGetResponse(GetResponseCallback, req);
    }

    private static void GetResponseCallback(IAsyncResult ar)
    {
        HttpWebRequest req = ar.AsyncState as HttpWebRequest;
        String result;
        using (WebResponse resp = req.EndGetResponse(ar))
        {
            using (StreamReader reader = new StreamReader(
                resp.GetResponseStream())
                )
            {
                result = reader.ReadToEnd();
            }
        }

        Console.WriteLine(result);
    }

Note the line:

req.ServicePoint.ConnectionLimit = 10;

It allows you to make more than 2 concurrent requests to the same service endpoint (See this post for more details). And you should pick the number carefully so as not to violate the term of usage (of the IMDB api service, if there is any).

这篇关于WebResponse类放缓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆