问题描述
我用C#来POST一个xml文件到服务器,并获得返回,但运行到GetRequestStream的时候特别慢,需要2-3秒才返回,为什么?有什么好方法提高效率吗?前段时间用C#写了一个往TCP某端口写数据,建立连接的时候也是特别慢,后来由于急,没办法,只有改用C++来做了,是不是C#就这本事?我才用C#不久,往高手指教。下面是我POST数据的代码,往高手指教usingSystem;usingSystem.Collections.Generic;usingSystem.Text;usingSystem.Data;usingSystem.Configuration;usingSystem.Web;//usingSystem.Web.Security;usingSystem.IO;usingSystem.Net;usingSystem.Security.Cryptography.X509Certificates;usingSystem.Net.Security;namespacepahttps{classProgram{staticstringinputFileName;staticstringoutputFileName;staticvoidMain(string[]args){inputFileName=args[0];outputFileName=args[1];StreamReaderDataReader=newStreamReader(inputFileName,UnicodeEncoding.GetEncoding("GB2312"));stringDataString=DataReader.ReadToEnd();DataReader.Close();HttpWebRequestHttpWReq=(HttpWebRequest)WebRequest.Create("https://www.myweb.com/invoke/receive?");HttpWReq.Method="POST";stringpostData=DataString;Encodingencoding=Encoding.GetEncoding(936);byte[]byte1=encoding.GetBytes(postData);HttpWReq.ContentType="text/xml";HttpWReq.ContentLength=byte1.Length;System.Net.ServicePointManager.CertificatePolicy=newMyPolicy();HttpWReq.ClientCertificates.Add(newX509Certificate2("my.pfx","1234"));StreamSendStream=null;Console.WriteLine("CreateGetReqstart"+System.DateTime.Now);SendStream=HttpWReq.GetRequestStream();////这里比较郁闷,需要2-3秒,就是这里效率低,求教Console.WriteLine("CreateGetReqend"+System.DateTime.Now);Console.WriteLine("poststart"+System.DateTime.Now);SendStream.Write(byte1,0,byte1.Length);SendStream.Close();Console.WriteLine("postend"+System.DateTime.Now);Console.WriteLine("CreateResponStart"+System.DateTime.Now);HttpWebResponseresponse=(HttpWebResponse)HttpWReq.GetResponse();StreamreceiveStream=response.GetResponseStream();//这里也比较慢。。。。。。Console.WriteLine("CreateResponEnd"+System.DateTime.Now);Console.WriteLine("receivestart"+System.DateTime.Now);StreamReaderreadStream=newStreamReader(receiveStream,Encoding.UTF8);Console.WriteLine("Responsestreamreceived.");Console.WriteLine("receiveend"+System.DateTime.Now);stringReceivedString="";ReceivedString=readStream.ReadToEnd();Console.Write(ReceivedString);readStream.Close();receiveStream.Close();StreamWriterReturnFile;ReturnFile=File.CreateText(outputFileName);ReturnFile.Write(ReceivedString);ReturnFile.Flush();ReturnFile.Close();}publicboolCheckValidationResult(objectsender,X509Certificatecertificate,X509Chainchain,SslPolicyErrorserrors){returntrue;}}publicclassMyPolicy:ICertificatePolicy{publicMyPolicy(){}publicboolCheckValidationResult(ServicePointsrvPoint,X509Certificatecertificate,WebRequestrequest,intcertificateProblem){returntrue;}}}
解决方案
解决方案二:
这两个地方都是需要远程回应时间的,时间长说明网络差,虽然我没这方面的困扰,也不排除可能C#真的烂
解决方案三:
谢谢答复,不过好像和网络没什么关系,因为同样的功能,我有用C++写,返回只是0.2秒,郁闷呀
解决方案四:
没高手解答吗?郁闷
解决方案五:
晕那,是这里没能能解决,还是就这样?
解决方案六:
呵呵,我和你遇到了一样的问题,其实很简单,就是代理惹的祸在GetRequestStream()之前加入HttpWReq.Proxy=null;就好了。.netframework2.0默认搜索代理。
解决方案七:
谢谢楼上,加上这句,果然快了不少
解决方案八:
语言没有什么烂不烂,要看应用场合,在说了,.NET是高封装的,有些东西是被封装在封装的,安全机制不同
解决方案九:
引用5楼wyware01的回复:
呵呵,我和你遇到了一样的问题,其实很简单,就是代理惹的祸在GetRequestStream()之前加入HttpWReq.Proxy=null;就好了。.netframework2.0默认搜索代理。
上面的学习了