0

I want to build a little remote tool for a school project. (XBMC)

At first I would say, that a string variable doesn´t include quotes, but \", which should picture quotes in a textbox and somewhere else. Firstly i mean C# makes quotes with that trick in a Webrequest too, but he doesn´t. Furthermore i can reach my device, but the ResponseReader doesn´t give any answer, but this isn´t important, because i can delete this part in need.

Here is an example for the variable Übergabe:

{\"jsonrpc\": \"2.0\", \"id\": 1, \"method\": \"Input.Up\"}

textBox2.Text includes the password and textBox1.Text includes the server.


How should i solve this?

I'm trying to make this WebRequest (method) work:

   private void Xorg (string Übergabe)
        {
            try
            {
                WebRequest MyRequest;
                MyRequest = WebRequest.Create(("http://root:") + (textBox2.Text) + ("@") + (textBox1.Text) + ("/jsonrpc?request=") + (Übergabe));
                MyRequest.Method = "GET";
                textBox3.Text = ("http://root:*****") + ("@") + (textBox1.Text) + ("/jsonrpc?request=") + (Übergabe);
                Stream ResponseStream;
                ResponseStream = MyRequest.GetResponse().GetResponseStream();
                StreamReader ResponseReader = new StreamReader(ResponseStream);

                while (ResponseReader.ReadLine() != null)
                {
                    listBox2.Items.Add(ResponseReader.ReadLine());
                }
            }
            catch
            { listBox2.Items.Add("Server antwortet nicht! / Fehlerhafte Eingabe!"); }
       }
  • You only need to escape characters with a backslash when you're writing code. The actual content of strings does not need to have escaped characters. Also, you don't need to wrap everything in parenthesis `(...)`... – Simon Whitehead Jul 01 '14 at 06:06
  • But I have written the part of the URL, which includes the quotes, in a string variable – user3792550 Jul 01 '14 at 07:28

1 Answers1

0

Use WebUtility.UrlEncode :

WebRequest MyRequest = WebRequest.Create(("http://root:") + (textBox2.Text) + ("@") + (textBox1.Text) + ("/jsonrpc?request=") + WebUtility.UrlEncode(Übergabe));

" must be converted to %22. With UrlEncode you ensure that all character are properly converted

Guillaume
  • 12,824
  • 3
  • 40
  • 48
  • Hello, the Webutility method doesn´t have this option in my version :( Are there any alternative methods? – user3792550 Jul 01 '14 at 11:43
  • @user3792550, Try with System.Uri.EscapeDataString(Übergabe) – Guillaume Jul 01 '14 at 12:16
  • I now works fine (System.Uri), but I can't reach the URL with the converted string.... – user3792550 Jul 01 '14 at 13:06
  • Here is the URL including the converted string: http://root:****@**.****.****.****:***/jsonrpc?request=%7B%22jsonrpc%22%3A%222.0%22%2C%22method%22%3A%22System.Shutdown%22%2C%22id%22%3A1%7D – user3792550 Jul 01 '14 at 13:10
  • @user3792550 Seems good, you may have another issue than encoding. Try to send a raw request with fiddler or typing the url in a browser and check the result. – Guillaume Jul 01 '14 at 13:38
  • Ahh, I know! I doesn't have the authentication for that, but why? Can you explain, why i can't use the authentication like root:password.. Please :) (It work´s in a browser) – user3792550 Jul 01 '14 at 16:18
  • @user3792550, ask another question on SO or search on Google ;) You are looking for Basic Authentication and WebRequest. – Guillaume Jul 01 '14 at 16:22