码迷,mamicode.com
首页 > 编程语言 > 详细

模态MFC框架窗口

时间:2020-08-09 16:47:13      阅读:84      评论:0      收藏:0      [点我收藏+]

标签:mes   循环   red   file   sla   内部实现   title   功能实现   下载   

using System;
using System.IO;
using System.Net;
using System.Text;

public class MyWebRequest
{
    private WebRequest request;
    private Stream dataStream;

    private string status;

    public String Status
    {
        get
        {
            return status;
        }
        set
        {
            status = value;
        }
    }

    /// <summary>
    /// 直接请求的
    /// </summary>
    /// <param name="url"></param>
    public MyWebRequest(string url)
    {
        // Create a request using a URL that can receive a post.

        request = WebRequest.Create(url);
    }


    /// <summary>
    /// 不带参数的get/post
    /// </summary>
    /// <param name="url"></param>
    /// <param name="method"></param>
    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");
        }
    }

    /// <summary>
    /// 带参数的get/post
    /// </summary>
    /// <param name="url"></param>
    /// <param name="method"></param>
    /// <param name="data"></param>
    /// <param name="contentType">用下面的HttpContentType类中的常量</param>
    public MyWebRequest(string url, string method, string data, string contentType)
        : 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";
        request.ContentType = contentType;

        // 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()
    {
        WebResponse response = request.GetResponse();
        this.Status = ((HttpWebResponse)response).StatusDescription;
        dataStream = response.GetResponseStream();
        StreamReader reader = new StreamReader(dataStream);
        string responseFromServer = reader.ReadToEnd();

        //释放
        reader.Close();
        dataStream.Close();
        response.Close();

        return responseFromServer;//返回数据
    }


    /// <summary>
    /// HTTP 内容类型(Content-Type)
    /// </summary>
    public class HttpContentType
    {
        /// <summary>
        /// 资源类型:普通文本
        /// </summary>
        public const string TEXT_PLAIN = "text/plain";

        /// <summary>
        /// 资源类型:JSON字符串
        /// </summary>
        public const string APPLICATION_JSON = "application/json";

        /// <summary>
        /// 资源类型:未知类型(数据流)
        /// </summary>
        public const string APPLICATION_OCTET_STREAM = "application/octet-stream";

        /// <summary>
        /// 资源类型:表单数据(键值对)
        /// </summary>
        public const string WWW_FORM_URLENCODED = "application/x-www-form-urlencoded";

        /// <summary>
        /// 资源类型:表单数据(键值对)。编码方式为 gb2312
        /// </summary>
        public const string WWW_FORM_URLENCODED_GB2312 = "application/x-www-form-urlencoded;charset=gb2312";

        /// <summary>
        /// 资源类型:表单数据(键值对)。编码方式为 utf-8
        /// </summary>
        public const string WWW_FORM_URLENCODED_UTF8 = "application/x-www-form-urlencoded;charset=utf-8";

        /// <summary>
        /// 资源类型:多分部数据
        /// </summary>
        public const string MULTIPART_FORM_DATA = "multipart/form-data";
    }
}

 

模态MFC框架窗口

标签:mes   循环   red   file   sla   内部实现   title   功能实现   下载   

原文地址:https://www.cnblogs.com/Dincat/p/13462980.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!