• 全部
  • C#综合技术
  • C#互联网桌面应用
  • AppLauncher
  • WinForm&WPF
  • C#开发新技术
  • 问答

GetRequestStream的效率为什么这么底?

yeyaodefengdi 2008-12-13 01:24:26
我用 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;
}
}
}
...全文
868 点赞 收藏 8
写回复
8 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
wanghao2979 2011-03-23
[Quote=引用 5 楼 wyware01 的回复:]
呵呵,我和你遇到了一样的问题,其实很简单,就是代理惹的祸
在 GetRequestStream() 之前加入
HttpWReq.Proxy = null;
就好了。
.net framework 2.0 默认搜索代理。
[/Quote]




上面的学习了
回复
tkggusraqk 2011-03-23
语言没有什么烂不烂,要看应用场合,在说了,.NET是高封装的,有些东西是被封装在封装的,安全机制不同
回复
PlayerYK 2009-09-09
谢谢楼上,加上这句,果然快了不少
回复
wyware01 2009-01-14
呵呵,我和你遇到了一样的问题,其实很简单,就是代理惹的祸
在 GetRequestStream() 之前加入
HttpWReq.Proxy = null;
就好了。
.net framework 2.0 默认搜索代理。
回复
yeyaodefengdi 2008-12-18
晕那,是这里没能能解决,还是就这样?
回复
yeyaodefengdi 2008-12-15
没高手解答吗? 郁闷
回复
sprc_lcl 2008-12-13
这两个地方都是需要远程回应时间的,时间长说明网络差,虽然我没这方面的困扰,也不排除可能C#真的烂
回复
yeyaodefengdi 2008-12-13
谢谢答复,不过好像和网络没什么关系,因为同样的功能,我有用C++写,返回只是0.2秒
,郁闷呀
回复
相关推荐
发帖
C#
创建于2007-09-28

10.5w+

社区成员

.NET技术 C#
申请成为版主
帖子事件
创建了帖子
2008-12-13 01:24
社区公告

让您成为最强悍的C#开发者