分润宝,专注各种代理商分润系统开发!完美兼容各支付公司产品代理商分润结算模式。
全国服务热线0592-5519259 在线咨询 微信咨询

分润宝 手机端

在线客服

技术文档 > 技术文档

C#POST请求自定义body内容传输

public static string SendMsg_Rpt(string body,string url)
    {
        string formUrl = url;
        //注意提交的编码 这边是需要改变的 这边默认的是Default:系统当前编码
        byte[] postData = System.Text.Encoding.UTF8.GetBytes(formUrl);
        byte[] postData2 = System.Text.Encoding.UTF8.GetBytes(body);
        // 设置提交的相关参数 
        System.Net.HttpWebRequest request = System.Net.WebRequest.Create(formUrl) as System.Net.HttpWebRequest;
        System.Text.Encoding myEncoding = System.Text.Encoding.UTF8;
        request.Method = "POST";
        request.KeepAlive = false;
        request.AllowAutoRedirect = true;
request.Headers.Add("api-key", "avPmtgL1GbFh7KTE4LxlsFoAKcY=");
        request.ContentType = "application/x-www-form-urlencoded";
        request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR  3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
        request.ContentLength = postData.Length;

        Stream outstream = null;

        outstream = request.GetRequestStream();

//自定义body内容postData2

        outstream.Write(postData2, 0, postData2.Length);

        // 提交请求数据 
        System.IO.Stream outputStream = request.GetRequestStream();
        outputStream.Write(postData, 0, postData.Length);
        outputStream.Close();

        System.Net.HttpWebResponse response;
        System.IO.Stream responseStream;
        System.IO.StreamReader reader;
        string srcString;
        response = request.GetResponse() as System.Net.HttpWebResponse;
        responseStream = response.GetResponseStream();
        reader = new System.IO.StreamReader(responseStream, System.Text.Encoding.GetEncoding("UTF-8"));
        srcString = reader.ReadToEnd();
        string result = srcString;   //返回值赋值
        reader.Close();
        return result;
    }

如果没有解决您的问题,请立即联系客服

声明:文章"C#POST请求自定义body内容传输"为分润宝原创文章,转载请注明出处,谢谢合作!

分润宝公众号

客服

微信客服

置顶