none
Equivalent RestSharp implementation RRS feed

  • Question

  • Hi,

    I'm in a hurry and doing ten things at once here so I need a favor, how would this code look if written using RestSharp?

        private void Button1_Click(object sender, System.EventArgs e)
            {
                ASCIIEncoding encoding = new ASCIIEncoding(); string postData =
                  "CUSTOMER_NBR=" + HttpUtility.UrlEncode("7000") +
                  "&MERCHANT_NBR=" + HttpUtility.UrlEncode("700010") +
                  "&DBAVAL_NBR=" + HttpUtility.UrlEncode("1") +
                  "&TERMINAL_NBR=" + HttpUtility.UrlEncode("1") +
                  "&TRAN_TYPE =" +HttpUtility.UrlEncode("CCM1") +
                   "&PRICE=" + HttpUtility.UrlEncode("12.95") +
                   "&TRANSFER_VAL=" + HttpUtility.UrlEncode("1") +
                   "&BATCH_MODE=" + HttpUtility.UrlEncode("5") +
                   "&USER_ENT_METH=" + HttpUtility.UrlEncode("X") +
                   "&ACCOUNT_DATA=" + HttpUtility.UrlEncode("4110231398111234") + "&EXPENSE_DATE="
                   + HttpUtility.UrlEncode("0605");
                byte[] data = encoding.GetBytes(postData);
                // Prepare web request... 
                HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("https://someservice.com");
                myRequest.Method = "POST"; myRequest.ContentType = "application/x-www-formurlencoded"; myRequest.ContentLength = data.Length;
                Stream newStream = myRequest.GetRequestStream();
                // Send the data. newStream.Write(data,0,data.Length); newStream.Close();
                HttpWebResponse loWebResponse = (HttpWebResponse)myRequest.GetResponse();
                Encoding enc = System.Text.Encoding.GetEncoding(1252); StreamReader
                loResponseStream =
                new StreamReader(loWebResponse.GetResponseStream(), enc);
                string lcHtml = loResponseStream.ReadToEnd(); loWebResponse.Close();
                loResponseStream.Close();
                TextBox1.Text = lcHtml;
            }
        }

    I'm not entirely clear on the exact way RestSharp does this (for example do I use AddParameter with ParameterType.RequestBody ?).

    Any help much appreciated.

    ThANKS


    PS: I need to use RestSharp as its our preferred library for this and we're doing this async, I just need to know the equivalent steps one would use - I can study this and get there myself but the time is a pain!

    Monday, January 21, 2019 5:59 PM

All replies

  • Hi Captain Kernel,

    Thank you for posting here.

    Since your question is more related to RestSharp, you could post a new thread in StackOverFlow.

    https://stackoverflow.com/questions/tagged/restsharp

    The Visual C# forum discuss and ask questions about the C# programming language, IDE, libraries, samples, and tools.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, January 22, 2019 5:49 AM