免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 442 | 回复: 0
打印 上一主题 下一主题

C#的HTTP请求客户端实现。 [复制链接]

论坛徽章:
59
2015七夕节徽章
日期:2015-08-24 11:17:25ChinaUnix专家徽章
日期:2015-07-20 09:19:30每周论坛发贴之星
日期:2015-07-20 09:19:42ChinaUnix元老
日期:2015-07-20 11:04:38荣誉版主
日期:2015-07-20 11:05:19巳蛇
日期:2015-07-20 11:05:26CU十二周年纪念徽章
日期:2015-07-20 11:05:27IT运维版块每日发帖之星
日期:2015-07-20 11:05:34操作系统版块每日发帖之星
日期:2015-07-20 11:05:36程序设计版块每日发帖之星
日期:2015-07-20 11:05:40数据库技术版块每日发帖之星
日期:2015-07-20 11:05:432015年辞旧岁徽章
日期:2015-07-20 11:05:44
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-21 08:41 |只看该作者 |倒序浏览
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Net;
  6. using System.IO;
  7. using System.Security.Cryptography.X509Certificates;
  8. using System.Net.Security;

  9. namespace Simulator
  10. {
  11.     class Simulator
  12.     {
  13.         public static string PostDataSSL(string purl, string str)
  14.         {
  15.             return PostData(purl, str, true);
  16.         }

  17.         public static string PostData(string url,string str){
  18.             return PostData(url, str, false);
  19.         }

  20.         public static string PostData(string purl,string str,bool sslAuth)
  21.           {
  22.               //System.Net.WebRequest req = System.Net.WebRequest.Create("your url");


  23.               //req.ContentType = "text/xml";

  24.               //req.Method = "POST";


  25.               //byte[] bytes = System.Text.Encoding.ASCII.GetBytes("Your Data");

  26.               //req.ContentLength = bytes.Length;

  27.               //os = req.GetRequestStream();

  28.               //os.Write(bytes, 0, bytes.Length);



  29.               //System.Net.WebResponse resp = req.GetResponse();

  30.               //if (resp == null) return;

  31.               //System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());


  32.               //str responsecontent = sr.ReadToEnd().Trim();



  33.               //if (isCer)

  34.               //{


  35.               // //需要Https验证

  36.               // System.Security.Cryptography.X509Certificates.X509Certificate cer;

  37.               // //System.Security.Cryptography.X509Certificates.X509Certificate cer = System.Security.Cryptography.X509Certificates.X509Certificate.CreateFromCertFile(pfxPath);

  38.               // if (String.IsNullOrEmpty(pfxPassword)) //是否证书加载是否需要密码

  39.               // cer = new X509Certificate(pfxPath);

  40.               // else

  41.               // cer = new X509Certificate(pfxPath, pfxPassword);

  42.               // webReq.ClientCertificates.Add(cer);


  43.               //}




  44.               try
  45.              {
  46.                  byte[] data = System.Text.Encoding.GetEncoding("UTF-8").GetBytes(str);
  47.                  // 准备请求

  48.                  HttpWebRequest req = (HttpWebRequest)WebRequest.Create(purl);
  49.                  if (sslAuth)
  50.                  {
  51.                      ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
  52.                  }
  53.                  //设置超时

  54.                  req.Timeout = 30000;
  55.                  req.Method = "POST";
  56.                  req.ContentType = "text/xml";
  57.                  req.ContentLength = data.Length;
  58.                  Stream stream = req.GetRequestStream();
  59.                  
  60.                  //req.ClientCertificates.Add(xCert);

  61.                  // 发送数据

  62.                  stream.Write(data, 0, data.Length);
  63.                  stream.Close();

  64.                  HttpWebResponse rep = (HttpWebResponse)req.GetResponse();
  65.                  Stream receiveStream = rep.GetResponseStream();
  66.                  Encoding encode = System.Text.Encoding.GetEncoding("UTF-8");
  67.                  // Pipes the stream to a higher level stream reader with the required encoding format.

  68.                  StreamReader readStream = new StreamReader(receiveStream, encode);

  69.                  Char[] read = new Char[256];
  70.                  int count = readStream.Read(read, 0, 256);
  71.                  StringBuilder sb = new StringBuilder("");
  72.                  while (count > 0)
  73.                  {
  74.                      String readstr = new String(read, 0, count);
  75.                      sb.Append(readstr);
  76.                      count = readStream.Read(read, 0, 256);
  77.                  }

  78.                  rep.Close();
  79.                  readStream.Close();

  80.                  return sb.ToString();

  81.              }
  82.              catch (Exception ex)
  83.              {
  84.                  return ex.Message;
  85.                 // ForumExceptions.Log(ex);

  86.              }
  87.          }

  88.     }
  89. }
http和https的区分也有。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP