Asp.Net HttpWebRequest class and the class HttpWebResponse

Related Bowen: https://www.cnblogs.com/xu-yi/p/10061342.html

Related Bowen: https: //www.cnblogs.com/zoujinhua/p/11313396.html

 

HttpWebRequest class: supporting properties and methods defined in WebRequst and other attributes and methods, enable the user to interact directly with the HTTP server.

HttpWebResponse categories: used to generate separate HTTP client application sends the HTTP request and receive an HTTP response.

public static string GetRequest(string serviceAddress, string strContent)
        {
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serviceAddress);
                request.Method = "POST";
                request.ContentType = "application/json";

                using (StreamWriter dataStream = new StreamWriter(request.GetRequestStream()))
                {
                    dataStream.Write(strContent);
                    dataStream.Close();
                }
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                string encoding = response.ContentEncoding;
                if (encoding == null || encoding.Length < 1)
                {
                    encoding = "UTF-8"; //默认编码  
                }
                StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));
                string= retString reader.ReadToEnd (); 

                return ; retString 
            } 
            the catch (Exception E) 
            { 
                return  " exception error occurs, external API service requests, error description: " + e.Message; 
            } 

        }

 

Guess you like

Origin www.cnblogs.com/mobaiyu/p/11606082.html