c#中的http Webrequest [英] http Webrequest in c#
问题描述
你好
我写了一个ac#desktop app,它使用webrequest / response将大型记录发布到远程网站。
当我在本地测试应用程序,将我的localhost网站指定为我的网址,该应用程序正常工作,但当我尝试连接到远程(实时)网站时,应用程序变得非常慢:上传2,000条记录需要两天以上。
我需要一个更可靠,更快捷的方式来发送这些记录。
(nb:我使用Backgroundworker进行多线程处理)
下面是我的代码的副本:
< pre lang =cs> public string PostDataToWeb(string postData,string url)
{
string retu;
WebRequest request = WebRequest.Create(url);
request.Method =& quot ; POST& quot ;;
byte [] byteArray = Encoding.UTF8.GetBytes(postData);
request.ContentType =&am p; quot; application / x-www-form-urlencoded& quot ;;
request.ContentLength = byteArray.Length;
使用(Stream dataStream = request.GetRequestStream())
{
dataStream.Write(byteArray,0,byteArray.Length);
}
使用(WebResponse response = request.GetResponse())
{
retu =(((HttpWebResponse)回复).StatusDescription);
}
返回retu;
}< / pre>
我现在将上述方法包含在循环中,循环遍历所有记录的数据表并逐行发送。
感谢预期。
tosin
hello
I wrote a c# desktop app that use webrequest/response to post large records to a remote website.
when i test the app locally, specifying my localhost site as my url, the app works fine but when i try to connect to a remote (live) website the application becomes very slow:it takes more than two days to upload 2,000 records.
Please I need a more reliable and faster way to send this records.
(n.b.:I am using Backgroundworker to multithread)
below is a copy of my code:
<pre lang="cs">public string PostDataToWeb(string postData, string url)
{
string retu;
WebRequest request = WebRequest.Create(url);
request.Method = "POST";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;
using (Stream dataStream = request.GetRequestStream())
{
dataStream.Write(byteArray, 0, byteArray.Length);
}
using (WebResponse response = request.GetResponse())
{
retu = (((HttpWebResponse)response).StatusDescription);
}
return retu;
}</pre>
I now included the above method in a loop that loop through a datatable for all the records and send them row by row.
thanks in anticipation.
tosin
推荐答案
如果它在本地更快,那么你的网络速度就是问题。除了使数据更小外,没有什么能比它更快。如果你拉链并发送拉链怎么办?
If it's faster locally, then your network speed is the issue. Nothing is going to make it faster except making your data smaller. What if you zip it and send a zip ?
这篇关于c#中的http Webrequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!