سؤال

I would like to know how to make the following HTTP call using C#.

http://user:pass@localhost:8080/jsonrpc?request={"jsonrpc":"2.0","method":"VideoLibrary.Scan"}

When I paste this url directly into my browser (and replace the user credentials & server location), it works as expected (my XBMC video library is updated). It relates specifically to the HTTP methods on this page:

http://wiki.xbmc.org/index.php?title=HOW-TO:Remotely_update_library

I'd like to know how to make this same successful call over HTTP using C# though.

هل كانت مفيدة؟

المحلول

Use this:

using (var webClient = new WebClient())
{
    var response = webClient.UploadString("http://user:pass@localhost:8080/jsonrpc", "POST", json);
}

نصائح أخرى

@Ganesh

I kept getting HTTP 401 Unauthorized messages until I added a reference to the network credentials (using http://username:password@server:port just didn't work for me)

using (var webClient = new WebClient())
{
  // Required to prevent HTTP 401: Unauthorized messages
  webClient.Credentials = new NetworkCredential(username, password);
  // API Doc: http://kodi.wiki/view/JSON-RPC_API/v6
  var json = "{\"jsonrpc\":\"2.0\",\"method\":\"GUI.ShowNotification\",\"params\":{\"title\":\"This is the title of the message\",\"message\":\"This is the body of the message\"},\"id\":1}";
  response = webClient.UploadString($"http://{server}:{port}/jsonrpc", "POST", json);
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top