博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

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

Posted on 2015-09-10 18:15  米粒3  阅读(526)  评论(0编辑  收藏  举报

/// <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() + "\n";

 


}

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() + "\n";

}

 

接收:

  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();

  }

  }

  }