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

  }

  }

  }

.Net HttpPost的发送和接收示例代码的更多相关文章

  1. (转载).Net HttpPost的发送和接收示例代码

    HttpPost在不同系统进行数据交互的时候经常被使用.它的最大好处在于直接,不像Webservice或者WCF需要wsdl作为一个双方的"中介".在安全性上,往往通过IP限制的方 ...

  2. javaMail 邮件发送和接收示例,支持正文图片、html、附件(转)

    转自:https://blog.csdn.net/star_fly4/article/details/52037587 一.RFC882文档简单说明 RFC882文档规定了如何编写一封简单的邮件(纯文 ...

  3. sendcloud golang 发送短信 示例代码

    package main import ( "fmt" "crypto/md5" "encoding/hex" "sort&quo ...

  4. Android Java使用JavaMail API发送和接收邮件的代码示例

    JavaMail是Oracle甲骨文开发的Java邮件类API,支持多种邮件协议,这里我们就来看一下Java使用JavaMail API发送和接收邮件的代码示例 使用Javamail发送邮件,必需的j ...

  5. Windows 和 Linux下使用socket下载网页页面内容(可设置接收/发送超时)的代码

    主要难点在于设置recv()与send()的超时时间,具体要注意的事项,请看代码注释部分,下面是代码: #include <stdio.h> #include <sys/types. ...

  6. ActiveMQ笔记(1):编译、安装、示例代码

    一.编译 虽然ActiveMQ提供了发布版本,但是建议同学们自己下载源代码编译,以后万一有坑,还可以尝试自己改改源码. 1.1 https://github.com/apache/activemq/r ...

  7. C# WebSocket 服务端示例代码 + HTML5客户端示例代码

    WebSocket服务端 C#示例代码 using System; using System.Collections.Generic; using System.Linq; using System. ...

  8. python开源项目及示例代码

    本页面是俺收集的各种 Python 资源,不定期更新. 下面列出的各种 Python 库/模块/工具,如果名称带超链接,说明是第三方的:否则是 Python 语言内置的. 1 算法 1.1 字符串处理 ...

  9. DICOM医学图像处理:DIMSE消息发送与接收“大同小异”之DCMTK fo-dicom mDCM

    背景: 从DICOM网络传输一文开始,相继介绍了C-ECHO.C-FIND.C-STORE.C-MOVE等DIMSE-C服务的简单实现,博文中的代码给出的实例都是基于fo-dicom库来实现的,原因只 ...

随机推荐

  1. 要学JavaScript!进来看吧,反正不花钱!~

    有一段时间没有更新自己的博客了,今天终于可以抽出时间和大家分享一下关于JavaScript了.有很多朋友在问我没有接触过会不会很难,不好理解 其实JavaScript的基本语法和Java的基本语法很相 ...

  2. Minimum Inversion Number_线段树||树状数组

    Problem Description The inversion number of a given number sequence a1, a2, ..., an is the number of ...

  3. Supermarket_贪心

    Description A supermarket has a set Prod of products on sale. It earns a profit px for each product ...

  4. Android SoundPool.play方法的音量与系统音量的关系

    Android中播放音频,除了使用MediaPlayer,还可以使用SoundPool.SoundPool使用音效池的概念来管理多个短促的音效,对于需要短促的提示音的场景特别合适. 通常调用Sound ...

  5. Bootstrap组件On和Off语法

    $(document).on('click.bs.carousel.data-api','td',function(e){}) $(document).off('.carousel.data-api' ...

  6. Objective-C--- 多态 、 协议

    1 编写交通工具程序 1.1 问题 本案例需要创建一个TRTransportation类,类中有一个方法叫print的方法,该方法默认输出 “显示交通工具信息”,这个类作为父类,派生出三个子类TRTa ...

  7. isFinite

    window.isFinite 如果 number 是有限数字(或可转换为有限数字),那么返回 true.否则,如果 number 是 NaN(非数字),或者是正.负无穷大的数,则返回 false

  8. JAVA存取PG大对象类型OID数据

    转载地址:http://my.oschina.net/liuyuanyuangogo/blog/151537 pg用大对象存储二进制数据的老文档:http://jdbc.postgresql.org/ ...

  9. matlab演奏最炫民族风的代码注释

    用Matlab来放音乐,和用单片机加蜂鸣器放音乐的原理都差不多,就是把连续的声音信号事先转换成用数字信号,然后用扬声器按照一定的节奏放出来.换句话说,演唱者是把声音经过麦克风转换成电信号,录音设备对这 ...

  10. soapUI 在多个测试套件 testsuite 里,多个testcase里传值如何实现

        1.首先 要添加一个全局 自定义变量 Custom Properties   2.用transfer property 将取来的值 放入到变量 getToken 里   3.在另一个testc ...