C#实现HTTP POST 做个记录备忘

最近工控软件经常需要做MES通信,基本上都是用Http Post实现,VC++做HTTP不如C#方便,以下是C#实现HttpPost的简单代码,记录以备遗忘

using System;
using System.IO;
using System.Net;
using System.Runtime.InteropServices;
using System.Text;

namespace SharpHttpPost
{
    [ComVisible(true)]
    [Guid("1F9DC565-A89F-4838-AF5A-5C2FFC0F3D66")]
    public interface IHttpPost
    {
        [DispId(1)]
        bool HttpPost(string url, string data, out string strReturn);
    }

    [ComVisible(true)]
    [Guid("95A96501-D92E-4135-884A-DD0B021760BC")]
    public class CHttpPost: IHttpPost
    {
        public bool HttpPost(string url, string data, out string strReturn)
        {
            try
            {
                byte[] msgBuffer = Encoding.UTF8.GetBytes(data);
                System.Net.HttpWebRequest httpWebRequest = null;
                httpWebRequest = (System.Net.HttpWebRequest)HttpWebRequest.Create(url);
                httpWebRequest.Method = "POST";
                httpWebRequest.ContentLength = msgBuffer.Length;
                httpWebRequest.Timeout = 3000;
				//httpWebRequest.ContentType = "text/xml; charset=utf-8";
                httpWebRequest.ContentType = "application/json;charset=utf-8";
                httpWebRequest.Accept = "*/*";
				//httpWebRequest.Connection = "Keep-Alive";

                Stream s = httpWebRequest.GetRequestStream();
                s.Write(msgBuffer, 0, msgBuffer.Length);
                msgBuffer = null;
                using (HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse())
                {
                    using (Stream rs = httpWebResponse.GetResponseStream())
                    {
                        StreamReader read = new StreamReader(rs);
                        strReturn = read.ReadToEnd();
                    }
                }
                return true;
            }
            catch (System.Exception ex)
            {
                strReturn = ex.Message;
                return false;
            }
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
using System; using System.Collections.Generic; using System.Text; using System.Net; using System.Net.Sockets; using System.Collections; using System.IO; using System.Text.RegularExpressions; using RE = System.Text.RegularExpressions.Regex; using System.Security.Cryptography.X509Certificates; /*************************************************************************************************************************************************** * *文件名:HttpProc.cs * *创建人:HeDaode * *日 期:2007.09.01 * *描 述:实现HTTP协议中的GET、POST请求 * *使 用:HttpProc.WebClient client = new HttpProc.WebClient(); client.Encoding = System.Text.Encoding.Default;//默认编码方式,根据需要设置其他类型 client.OpenRead("http://www.baidu.com");//普通get请求 MessageBox.Show(client.RespHtml);//获取返回的网页源代码 client.DownloadFile("http://www.codepub.com/upload/163album.rar",@"C:\163album.rar");//下载文件 client.OpenRead("http://passport.baidu.com/?login","username=zhangsan&password=123456");//提交表单,此处是登录百度的示例 client.UploadFile("http://hiup.baidu.com/zhangsan/upload", @"file1=D:\1.mp3");//上传文件 client.UploadFile("http://hiup.baidu.com/zhangsan/upload", "folder=myfolder&size=4003550",@"file1=D:\1.mp3");//提交含文本域和文件域的表单 *****************************************************************************************************************************************************/ namespace HttpProc { /// ///上传事件委托 /// /// /// public delegate void WebClientUploadEvent(object sender, HttpProc.UploadEventArgs e); /// ///下载事件委托 /// /// /// public delegate void WebClientDownloadEvent(object sender, HttpProc.DownloadEventArgs e); /// ///上传事件参数 /// public struct UploadEventArgs { /// ///上传数据总大小 ///
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值