GetRequestStream的效率为什么这么底?
我用 C#来POST 一个xml文件到服务器,并获得返回, 但运行到 GetRequestStream 的时候特别慢,需要2-3秒才返回,
为什么?有什么好方法提高效率吗? 前段时间用C#写了一个往TCP某端口写数据,建立连接的时候也是特别慢,后来由于急,
没办法,只有改用C++来做了,是不是C#就这本事? 我才用C#不久,往高手指教。
下面是我POST数据的代码,往高手指教
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Configuration;
using System.Web;
//using System.Web.Security;
using System.IO;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
namespace pahttps
{
class Program
{
static string inputFileName;
static string outputFileName;
static void Main(string[] args)
{
inputFileName = args[0];
outputFileName = args[1];
StreamReader DataReader = new StreamReader(inputFileName,UnicodeEncoding.GetEncoding("GB2312"));
string DataString = DataReader.ReadToEnd();
DataReader.Close();
HttpWebRequest HttpWReq = (HttpWebRequest)WebRequest.Create("https://www.myweb.com/invoke/receive?");
HttpWReq.Method = "POST";
string postData = DataString;
Encoding encoding = Encoding.GetEncoding(936);
byte[] byte1 = encoding.GetBytes(postData);
HttpWReq.ContentType = "text/xml";
HttpWReq.ContentLength = byte1.Length;
System.Net.ServicePointManager.CertificatePolicy = new MyPolicy();
HttpWReq.ClientCertificates.Add(new X509Certificate2("my.pfx", "1234"));
Stream SendStream=null;
Console.WriteLine("Create GetReq start " + System.DateTime.Now);
SendStream = HttpWReq.GetRequestStream(); ////这里比较郁闷,需要2-3秒,就是这里效率低,求教
Console.WriteLine("Create GetReq end " + System.DateTime.Now);
Console.WriteLine("post start " + System.DateTime.Now);
SendStream.Write(byte1, 0, byte1.Length);
SendStream.Close();
Console.WriteLine("post end " + System.DateTime.Now);
Console.WriteLine("Create Respon Start " + System.DateTime.Now);
HttpWebResponse response = (HttpWebResponse)HttpWReq.GetResponse();
Stream receiveStream = response.GetResponseStream(); //这里也比较慢。。。。。。
Console.WriteLine("Create Respon End " + System.DateTime.Now);
Console.WriteLine("receive start " + System.DateTime.Now);
StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);
Console.WriteLine("Response stream received.");
Console.WriteLine("receive end " + System.DateTime.Now);
string ReceivedString = "";
ReceivedString = readStream.ReadToEnd();
Console.Write(ReceivedString);
readStream.Close();
receiveStream.Close();
StreamWriter ReturnFile;
ReturnFile = File.CreateText(outputFileName);
ReturnFile.Write(ReceivedString);
ReturnFile.Flush();
ReturnFile.Close();
}
public bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
{
return true;
}
}
public class MyPolicy : ICertificatePolicy
{
public MyPolicy()
{
}
public bool CheckValidationResult(ServicePoint srvPoint, X509Certificate certificate, WebRequest request, int certificateProblem)
{
return true;
}
}
}