zoukankan      html  css  js  c++  java
  • C# 实现GET/POST请求

    详细代码如下

     public class MyWebRequest
        {
            private WebRequest request;
            private Stream dataStream;
    
            private string status;
    
            public String Status
            {
                get
                {
                    return status;
                }
                set
                {
                    status = value;
                }
            }
    
            public MyWebRequest(string url)
            {
                // Create a request using a URL that can receive a post.
    
                request = WebRequest.Create(url);
            }
    
            public MyWebRequest(string url, string method)
                : this(url)
            {
    
                if (method.Equals("GET") || method.Equals("POST"))
                {
                    // Set the Method property of the request to POST.
                    request.Method = method;
                }
                else
                {
                    throw new Exception("Invalid Method Type");
                }
            }
    
            public MyWebRequest(string url, string method, string data)
                : this(url, method)
            {
    
                // Create POST data and convert it to a byte array.
                string postData = data;
                byte[] byteArray = Encoding.UTF8.GetBytes(postData);
    
                // Set the ContentType property of the WebRequest.
                request.ContentType = "application/x-www-form-urlencoded";
    
                // Set the ContentLength property of the WebRequest.
                request.ContentLength = byteArray.Length;
    
                // Get the request stream.
                dataStream = request.GetRequestStream();
    
                // Write the data to the request stream.
                dataStream.Write(byteArray, 0, byteArray.Length);
    
                // Close the Stream object.
                dataStream.Close();
    
            }
    
            public string GetResponse()
            {
                // Get the original response.
                WebResponse response = request.GetResponse();
    
                this.Status = ((HttpWebResponse)response).StatusDescription;
    
                // Get the stream containing all content returned by the requested server.
                dataStream = response.GetResponseStream();
    
                // Open the stream using a StreamReader for easy access.
                StreamReader reader = new StreamReader(dataStream);
    
                // Read the content fully up to the end.
                string responseFromServer = reader.ReadToEnd();
    
                // Clean up the streams.
                reader.Close();
                dataStream.Close();
                response.Close();
    
                return responseFromServer;
            }
        }
  • 相关阅读:
    [SNOI2019]数论
    2018-8-10-C#-写系统日志
    2018-8-10-C#-写系统日志
    2019-3-1-C#-double-好用的扩展
    2019-3-1-C#-double-好用的扩展
    2019-8-31-dotnet-Framework-源代码-·-Ink
    2019-8-31-dotnet-Framework-源代码-·-Ink
    2019-8-31-How-to-fix-nuget-Unrecognized-license-type-MIT-when-pack
    2019-8-31-How-to-fix-nuget-Unrecognized-license-type-MIT-when-pack
    2018-9-30-C#-传入-params-object-长度
  • 原文地址:https://www.cnblogs.com/Adger/p/10856771.html
Copyright © 2011-2022 走看看