Search code examples
c#postresponsewebrequest

C# WebRequest POST and get a specific line - how to display it in label


here is the picture of it .. look here also /.. http://prntscr.com/5wadok

            string fbid = stTextBox1.Text;

            string ukey = stTextBox2.Text;

            string jumlah = "4";


            ASCIIEncoding encoding = new ASCIIEncoding();
            string postData = "fbid=" + fbid + "&ukey=" + ukey + "&jumlah=" + jumlah; ;
            byte[] data = encoding.GetBytes(postData);


            WebRequest request = WebRequest.Create("http://dcvn-full.ga/test/dcgems.php");
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = data.Length;

            Stream stream = request.GetRequestStream();
            stream.Write(data, 0, data.Length);
            stream.Close();

            WebResponse response = request.GetResponse();
            stream = response.GetResponseStream();

            StreamReader sr = new StreamReader(stream);
            //MessageBox.Show(sr.ReadToEnd());
            stLabel4.Text = (sr.ReadLine());


            sr.Close();

            stream.Close();

I need to read the line21to25 and show it in the stlabel.5 with this command or any other , can u help me??


Solution

  • I modified your code as follows. Hopefully it will solve your problem.

            string fbid = stTextBox1.Text;
    
            string ukey = stTextBox2.Text;
    
            string jumlah = "4";
    
    
            ASCIIEncoding encoding = new ASCIIEncoding();
            string postData = "fbid=" + fbid + "&ukey=" + ukey + "&jumlah=" + jumlah; ;
            byte[] data = encoding.GetBytes(postData);
    
    
            WebRequest request = WebRequest.Create("http://dcvn-full.ga/test/dcgems.php");
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = data.Length;
    
            using (Stream stream = request.GetRequestStream())
            {
                stream.Write(data, 0, data.Length);
            }
    
            WebResponse response = request.GetResponse();
            stream = response.GetResponseStream();
    
            StringBuilder sb = new StringBuilder();
            using (StreamReader sr = new StreamReader(stream))
            {
                int count=1;
                while(sr.EndOfStream)
                {
                    string line = sr.ReadLine();
                    if(count>=21 && count<=25)
                    {
                        sb.AppendLine(line);
                    }
                    count++;
                    if (count > 25)
                        break;
                }
                stLabel4.Text = (sb.ToString());
            }