• .Net HttpPost的发送和接收示例代码


    /// <summary>
    /// 模拟http 发送post或get请求
    /// </summary>
    /// <param name="Url"></param>
    /// <param name="postDataStr"></param>
    /// <returns></returns>
    public string HttpPost(string url, string data)
    {
    string returnData = null;
    try
    {
    byte[] buffer = Encoding.UTF8.GetBytes(data);
    HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(url);
    webReq.Method = "POST";
    webReq.ContentType = "application/x-www-form-urlencoded";
    webReq.ContentLength = buffer.Length;
    Stream postData = webReq.GetRequestStream();
    postData.Write(buffer, 0, buffer.Length);
    postData.Close();
    //HttpWebResponse webResp = (HttpWebResponse)webReq.GetResponse();
    HttpWebResponse webResp;
    try
    {
    webResp = (HttpWebResponse)webReq.GetResponse();
    }
    catch (WebException ex)
    {
    webResp = (HttpWebResponse)ex.Response;
    }


    Stream answer = webResp.GetResponseStream();
    StreamReader answerData = new StreamReader(answer);
    returnData = answerData.ReadToEnd();
    }
    catch (Exception ex)
    {
    //Response.Write(ex.Message);
    return ex.Message;
    }
    return returnData.Trim() + " ";


    }

    public static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
    { // 总是接受
    return true;
    }
    public string HttpGet(string PageUrl)
    {
    try
    {
    ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);
    WebRequest request = WebRequest.Create(PageUrl);
    WebResponse response = request.GetResponse();
    Stream resStream = response.GetResponseStream();
    Encoding encode = Encoding.GetEncoding("utf-8");
    //Encoding encode = Encoding.GetEncoding("gb2312");
    StreamReader sr = new StreamReader(resStream, encode);
    string retstr = sr.ReadToEnd();
    resStream.Close();
    sr.Close();
    return retstr;
    }
    catch (Exception e)
    {
    return e.Message;
    }
    }


    /// <summary>
    /// 模拟http 发送post或get请求 传递Json内容
    /// </summary>
    /// <param name="Url"></param>
    /// <param name="postDataStr"></param>
    /// <returns></returns>
    public string HttpPostJson(string url, string data)
    {
    string returnData = null;
    try
    {
    byte[] buffer = Encoding.UTF8.GetBytes(data);
    HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(url);
    webReq.Method = "POST";
    webReq.ContentType = "application/json";
    webReq.Accept = "application/json";
    webReq.ContentLength = buffer.Length;
    Stream postData = webReq.GetRequestStream();
    postData.Write(buffer, 0, buffer.Length);
    postData.Close();
    //HttpWebResponse webResp = (HttpWebResponse)webReq.GetResponse();
    HttpWebResponse webResp;
    try
    {
    webResp = (HttpWebResponse)webReq.GetResponse();
    }
    catch (WebException ex)
    {
    webResp = (HttpWebResponse)ex.Response;
    }


    Stream answer = webResp.GetResponseStream();
    StreamReader answerData = new StreamReader(answer);
    returnData = answerData.ReadToEnd();
    }
    catch (Exception ex)
    {
    //Response.Write(ex.Message);
    return ex.Message;
    }
    return returnData.Trim() + " ";

    }

    接收:

      using System;

      using System.Text;

      namespace HttpPostDemo.Receive

      {

      public partial class Receiver : System.Web.UI.Page

      {

      protected void Page_Load(object sender, EventArgs e)

      {

      var inputStream = Request.InputStream;

      var strLen = Convert.ToInt32(inputStream.Length);

      var strArr = new byte[strLen];

      inputStream.Read(strArr, 0, strLen);

      var requestMes = Encoding.UTF8.GetString(strArr);

      Response.Write(requestMes);

      Response.End();

      }

      }

      }

  • 相关阅读:
    你的想像力智商有多高?
    Visual FoxPro 9.0 发布
    Google的社会网络
    女人永远是对的
    如何保存ICQ聊天历史
    7 30 个人赛
    Linux下利用文件描述符恢复的成功失败实验
    蓝鲸社区版部署
    Oracle 10.2.0.5升级至11.2.0.4
    手动创建Oracle实例
  • 原文地址:https://www.cnblogs.com/mili3/p/4798762.html
Copyright © 2020-2023  润新知