T
T
Tony13372017-05-22 18:12:19
In contact with
Tony1337, 2017-05-22 18:12:19

C# (WinForms) - How to make a GET request (VK API)?

Hello! I am a beginner programmer. I write in C#, recently moved from the console to WinForms. I started to study the VK API, I managed to do the authorization, and now I have problems using the methods. I don't understand how to correctly make a GET request and display the result on a TextBox, for example news (NewsFeed). Thanks in advance to everyone who helps!
PS If anything, write in more detail, because I have been programming for 1-2 months

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Artem, 2017-05-22
@devspec

Use WebClient

P
Peter, 2017-05-22
@petermzg

GET request is when request parameters are passed via URL
For example:
Parameters always start after the "?"
here the parameters with the names login and password are passed, the
separator is "&"

I
Ilfar, 2017-05-24
@Ilfar_sif

not long ago worked with get-request. Maybe something will help.
this method accepts url and data to send. and returns a response from the server:

public string GET(string sUrl, string Data)
        {
        
            string returnData = null;
            string d = Data;
            try
            {
             TcpClient newClient = new TcpClient();
             //тут к примеру подключаемся к http://192.168.16.254:8080/blink?f=2
             newClient.Connect(sUrl, 8080);
             byte[] sendBytes = System.Text.Encoding.ASCII.GetBytes(d);
             NetworkStream tcpStream = newClient.GetStream();          
             tcpStream.Write(sendBytes, 0, sendBytes.Length);
             byte[] bytes = new byte[newClient.ReceiveBufferSize];
             int bytesRead = tcpStream.Read(bytes, 0, newClient.ReceiveBufferSize);
             
             // Строка, содержащая ответ от сервера
             returnData = Encoding.UTF8.GetString(bytes);
            //закрываем подключение
            newClient.Close();

        }
            catch (SocketException ex)
            {
                 //если что-то пойдет не так, тут покажет ошибку
                MessageBox.Show(ex.ToString());
            }
            //возвращает ответ от сервера.
            return returnData;
        }

The Get() method can be called on a button click:
private void button1_Click(object sender, EventArgs e)
        {
                   //тут создаю экземпляр класса, потому что у меня метод get() находится в классе Class_Socket
                  Class_Socket cl_s = new Class_Socket();
                    //тут вызываю метод и одновременно присваиваю к переменной 'а' ответ от сервера 
                     //url у меня //public String URL = "http://192.168.16.254:8080/blinkSs"+"?";
                     //public String r="тут данные";
                     //общии url будет выглядит так:http://192.168.16.254:8080/blinkSs?тут данные

                    String a = cl_s.GET(URL, r);  //"String a" можно куда-нибудь вывести и посмотреть, что из себя 
                     //представляет, а потом можно парсить.
        }

if the get() method is in the same class you are calling from, then you can simply write: String a = GET(URL, r);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question