top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to do CRUD operations on an external URL in C#?

0 votes
269 views
How to do CRUD operations on an external URL in C#?
posted Apr 21, 2017 by Pooja Bhanout

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

You can use the following code for calling the external URL and posting the request. You can change the POST method to GET,PUT or DELETE depending on your requirement,

   //  CREATE REQUEST
        HttpWebRequest Request = (HttpWebRequest)WebRequest.Create("https://xyz.com);
        Request.Method = "POST";
        Request.ContentType = "application/json";
        Request.Headers.Add(string.Format("Authorization:{0}", "AVCNV")); 
        Stream dataStream = Request.GetRequestStream();
        dataStream.Write(byteArray, 0, byteArray.Length);
        dataStream.Close();
        //  GET RESPONSE
        try
        {
            WebResponse Response = Request.GetResponse();
            HttpStatusCode ResponseCode = ((HttpWebResponse)Response).StatusCode;
            if (ResponseCode.Equals(HttpStatusCode.Unauthorized) || ResponseCode.Equals(HttpStatusCode.Forbidden))
            {
                var error = "Unauthorized - need new token";
            }
            else if (!ResponseCode.Equals(HttpStatusCode.OK))
            {
                var error = "Response from web service isn't OK";
            }

            StreamReader Reader = new StreamReader(Response.GetResponseStream());
            string responseLine = Reader.ReadToEnd();
            Reader.Close();
            return true;
        }
        catch (Exception ex)
        {
        }
        return false;
answer May 3, 2017 by Shweta Singh
...