How to get data through different request methods of HTTP protocol in C#

private string GetRequest(string url)

        {

           string content = "";

           Uri  httpURL =  new  Uri (url);

           /// The HttpWebRequest class inherits from WebRequest and does not have its own constructor. It needs to be created through the Creat method of WebRequest , and the forced type conversion is performed.   

            HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(httpURL);

            httpReq.Credentials = new NetworkCredential("***""****");

           /// Create HttpWebResponse through the GetResponse() method of HttpWebRequest, and force type conversion  

            HttpWebResponse httpResp = (HttpWebResponse)httpReq.GetResponse();

           /// The GetResponseStream() method gets the data stream of the HTTP response , and tries to get the web page content specified in the URL  

            /// If the content of the web page is successfully obtained, it will be returned in the form of System.IO.Stream . If it fails, a ProtoclViolationException error will be generated. The correct approach here is to place the following code in a try block. Simple handling here   

            Stream respStream = httpResp.GetResponseStream();

           /// The returned content is in the form of Stream , so you can use the StreamReader class to get the content of GetResponseStream and use the  

            //The Read method of the StreamReader class reads the content of each line of the source code of the web page in turn until the end of the line (read encoding format: UTF8 )  

            StreamReader respStreamReader = new StreamReader(respStream, Encoding.UTF8);

            content = respStreamReader.ReadToEnd();

           return content;

        }

       public string PostRequest(string Paras, string Url)

        {

           string result = "";

           HttpWebRequest request = WebRequest.Create(Url) as HttpWebRequest;

            request.Method = "POST";

            request.ContentType = "application/json";

           httpReq.Credentials = new NetworkCredential("***""****");

           string data = Paras;

           byte[] byteData = UTF8Encoding.UTF8.GetBytes(data.ToString());

            request.ContentLength = byteData.Length;

 

           using (Stream postStream = request.GetRequestStream())

            {

                postStream.Write(byteData, 0, byteData.Length);

            }

 

           using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)

            {

               StreamReader reader = new StreamReader(response.GetResponseStream());

                result = reader.ReadToEnd();

            }

           return result;

        }

       public string DeleteRequest(string Url)

        {

           HttpWebRequest myrequest = (HttpWebRequest)WebRequest.Create(Url);

            myrequest.Method = "DELETE";

            httpReq.Credentials = new NetworkCredential("***""****");

           HttpWebResponse myResponse = (HttpWebResponse)myrequest.GetResponse();

           StreamReader reader = new StreamReader(myResponse.GetResponseStream(),Encoding.UTF8);

           string Return = reader.ReadToEnd();

            reader.Close();

           return Return;

        }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325559148&siteId=291194637