Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6658581
  • 博文数量: 915
  • 博客积分: 17977
  • 博客等级: 上将
  • 技术积分: 8846
  • 用 户 组: 普通用户
  • 注册时间: 2005-08-26 09:59
个人简介

一个好老好老的老程序员了。

文章分类

全部博文(915)

文章存档

2022年(9)

2021年(13)

2020年(10)

2019年(40)

2018年(88)

2017年(130)

2015年(5)

2014年(12)

2013年(41)

2012年(36)

2011年(272)

2010年(1)

2009年(53)

2008年(65)

2007年(47)

2006年(81)

2005年(12)

分类: 嵌入式

2011-06-09 14:06:04

  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的区分也有。
阅读(2627) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~