ASP.NET impersonation http perform GET / POST requests

Memorandum: C # two kinds of POST requests the external interface means.

 

1. Request parameter by FormBody form:

        /// transmission request  
         ///  </ Summary>   
        ///  <param name = "URL"> request address </ param>   
        ///  <param name = "the sendData"> parameter format "name = Joe Smith & pass = 123456 " </ param>   
        ///  <Returns> </ Returns>   
        public  static  String RequestWebAPI ( String URL, String the sendData) 
        { 
            String backMsg = " " ;
             the try 
            { 
                System.Net.WebRequest httpRquest = System.Net.HttpWebRequest.Create ( URL); 
                httpRquest.Method = "The POST " ;
                 // This line is critical, not the background will result parameter set ContentType not obtain the value   
                httpRquest.ContentType = " file application / X-WWW-form-urlencoded; charset = UTF-. 8 " ;
                 byte [] = dataArray is the System .Text.Encoding.UTF8.GetBytes (the sendData);
                 // httpRquest.ContentLength = to dataArray.length;   
                System.IO.Stream The requestStream = null ;
                 IF ( String .IsNullOrWhiteSpace (the sendData) == to false ) 
                { 
                    requestStream = httpRquest.GetRequestStream();
                    requestStream.Write(dataArray, 0, dataArray.Length);
                    requestStream.Close();
                }
                System.Net.WebResponse response = httpRquest.GetResponse();
                System.IO.Stream responseStream = response.GetResponseStream();
                System.IO.StreamReader reader = new System.IO.StreamReader(responseStream, System.Text.Encoding.UTF8);
                backMsg = reader.ReadToEnd();


                reader.Close();
                reader.Dispose();


                requestStream.Dispose();
                responseStream.Close();
                responseStream.Dispose();
            }
            catch (Exception)
            {
                throw;
            }
            return backMsg;
        }  

 

2. json request parameters in the form of:

    public string GetPage(string posturl, string postData)
    {
        Stream outstream = null;
        Stream instream = null;
        StreamReader sr = null;
        HttpWebResponse response = null;
        HttpWebRequest request = null;
        Encoding encoding = Encoding.UTF8;
        byte[] data = encoding.GetBytes(postData);
        try
        {
            // 设置参数
            request = WebRequest.Create(posturl) as HttpWebRequest;
            CookieContainer cookieContainer = new CookieContainer();
            request.CookieContainer = cookieContainer;
            request.AllowAutoRedirect = true;
            request.Method = "POST";
            request.ContentType = "application/json";
            request.ContentLength = data.Length;
            outstream = request.GetRequestStream();
            outstream.Write (Data, 0 , data.length); 
            outstream.Close (); 
            // send the request and obtain the corresponding response data 
            Response = request.GetResponse () AS the HttpWebResponse;
             // until request.GetResponse () procedure began to Post target page transmission request 
            inStream = response.GetResponseStream (); 
            SR = new new the StreamReader (inStream, encoding);
             // returns the results page (html) Code 
            String Content = sr.ReadToEnd ();
             String ERR = String .Empty;
             //    Response.Write (Content); 
            return Content; 
        }
        catch (Exception ex)
        {
            string err = ex.Message;
            return string.Empty;
        }
    }

 

Guess you like

Origin www.cnblogs.com/jerry-liu/p/11777077.html