Reputation: 39
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);
}
Upvotes: 0
Views: 1478
Reputation: 469
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).
Upvotes: 0
Reputation: 22021
I'm afraid your service can run no faster than the speed of the imdb api lookups, plus some overhead in relation to the bandwidth between you and the imdb service, plush a tiny overhead in calling HttpWebRequest.GetResponse.
Probably the only way you could speed up your service would be to have it hosted in the same building as imdb.
Upvotes: 2