0

映画に関する情報を取得するために API を使用しようとしています。実際に動作するこのAPI を使用しています。

唯一のことは、それが遅くなる方法であり、それを行うためのより速い方法があるかどうか疑問に思いましたか? これは私にとってかなり新しいことだと言わざるを得ません。ここにいくつかのコードがあります。

    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);
    }
4

2 に答える 2

2

残念ながら、あなたのサービスは imdb api ルックアップの速度よりも速く実行することはできず、加えて、あなたと imdb サービス間の帯域幅に関連するいくらかのオーバーヘッド、さらにHttpWebRequest.GetResponseを呼び出す際のわずかなオーバーヘッドが追加されます。

おそらく、サービスを高速化できる唯一の方法は、サービスを imdb と同じ建物でホストすることです。

于 2012-04-17T13:42:53.550 に答える
0

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).

于 2012-04-17T15:02:07.350 に答える