为什么使用 WebRequest 发送 post 数据需要这么长时间? [英] Why does sending post data with WebRequest take so long?

查看:31
本文介绍了为什么使用 WebRequest 发送 post 数据需要这么长时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在创建一个 C# 应用程序以连接到 php/MySQL 在线系统.应用程序需要将发布数据发送到脚本并获得响应.

当我发送以下数据时

<前>用户名=测试&密码=测试

我收到以下回复...

<前>开始请求于 22/04/2010 12:15:42完成创建请求:采取 00:00:00.0570057传输数据于 22/04/2010 12:15:42传输数据:采取了 00:00:06.9316931 <<--在 22/04/2010 12:15:49 得到回复得到回应 00:00:00.0360036完成响应 00:00:00.0360036整个通话时间为 00:00:07.0247024

如您所见,实际将数据发送到脚本需要 6 秒,我已经完成了进一步测试,再见,从 telnet 发送数据,并将发布数据从本地文件发送到 url,它们甚至不需要一秒钟所以这不是网站上托管脚本的问题.

为什么是两个简单的字符串,传输数据需要6秒?

我使用自定义类发送数据

class httppostdata{WebRequest 请求;WebResponse 响应;公共字符串senddata(字符串url,字符串postdata){var start = DateTime.Now;Console.WriteLine("开始请求" + start.ToString());//创建对传入参数的 url 的请求request = (WebRequest)WebRequest.Create(url);//设置发布方法request.Method = "POST";//设置内容类型和内容长度request.ContentType = "application/x-www-form-urlencoded";request.ContentLength = postdata.Length;//将帖子数据转换为字节数组byte[] byteData = Encoding.UTF8.GetBytes(postdata);var end1 = DateTime.Now;Console.WriteLine("完成创建请求:接受了" + (end1 - start));var start2 = DateTime.Now;Console.WriteLine("正在传输数据" + start2.ToString());//获取请求流并将数据写入其中流数据流 = request.GetRequestStream();dataStream.Write(byteData, 0, byteData.Length);数据流.关闭();var end2 = DateTime.Now;Console.WriteLine("传输的数据:取了" + (end2 - start2));//获取响应var start3 = DateTime.Now;Console.WriteLine("正在获取响应" + start3.ToString());响应 = request.GetResponse();//Console.WriteLine(((WebResponse)response).StatusDescription);dataStream = response.GetResponseStream();StreamReader reader = new StreamReader(dataStream);var end3 = DateTime.Now;Console.WriteLine("得到响应" + (end3 - start3));//读取响应字符串 serverresponse = reader.ReadToEnd();var end3a = DateTime.Now;Console.WriteLine("完成响应" + (end3a - start3));Console.WriteLine("整个通话结束了" + (end3a - start));//Console.WriteLine(serverresponse);reader.Close();数据流.关闭();response.Close();返回服务器响应;}}

我用它来称呼它

private void btnLogin_Click(object sender, EventArgs e){//字符串后数据;if (txtUsername.Text.Length <3 || txtPassword.Text.Length <3){MessageBox.Show("缺少您的用户名或密码.");}别的{string postdata = "username=" + txtUsername.Text +"&password=" + txtPassword.Text;httppostdata myPost = new httppostdata();string response = myPost.senddata("http://www.domainname.com/scriptname.php", postdata);MessageBox.Show(响应);}}

解决方案

确保将 WebRequest 的代理属性显式设置为 null,否则它会尝试自动检测代理设置,这可能需要一些时间.

I am currently creating a C# application to tie into a php / MySQL online system. The application needs to send post data to scripts and get the response.

When I send the following data

username=test&password=test  

I get the following responses...

Starting request at 22/04/2010 12:15:42  
Finished creating request : took 00:00:00.0570057  
Transmitting data at 22/04/2010 12:15:42  
Transmitted the data : took 00:00:06.9316931       <<--
Getting the response at 22/04/2010 12:15:49  
Getting response 00:00:00.0360036  
Finished response 00:00:00.0360036  
Entire call took 00:00:07.0247024  

As you can see it is taking 6 seconds to actually send the data to the script, I have done further testing bye sending data from telnet and by sending post data from a local file to the url and they dont even take a second so this is not a problem with the hosted script on the site.

Why is it taking 6 seconds to transmit the data when it is two simple strings?

I use a custom class to send the data

class httppostdata
{
    WebRequest request;
    WebResponse response;

    public string senddata(string url, string postdata)
    {
        var start = DateTime.Now;
        Console.WriteLine("Starting request at " + start.ToString());

        // create the request to the url passed in the paramaters
        request = (WebRequest)WebRequest.Create(url);


        // set the method to post
        request.Method = "POST";
        // set the content type and the content length
        request.ContentType = "application/x-www-form-urlencoded";
        request.ContentLength = postdata.Length;
        // convert the post data into a byte array
        byte[] byteData = Encoding.UTF8.GetBytes(postdata);
        var end1 = DateTime.Now;
        Console.WriteLine("Finished creating request : took " + (end1 - start));

        var start2 = DateTime.Now;
        Console.WriteLine("Transmitting data at " + start2.ToString());
        // get the request stream and write the data to it
        Stream dataStream = request.GetRequestStream();
        dataStream.Write(byteData, 0, byteData.Length);
        dataStream.Close();
        var end2 = DateTime.Now;
        Console.WriteLine("Transmitted the data : took " + (end2 - start2));


        // get the response
        var start3 = DateTime.Now;
        Console.WriteLine("Getting the response at " + start3.ToString());


        response = request.GetResponse();
        //Console.WriteLine(((WebResponse)response).StatusDescription);
        dataStream = response.GetResponseStream();
        StreamReader reader = new StreamReader(dataStream);
        var end3 = DateTime.Now;
        Console.WriteLine("Getting response " + (end3 - start3));

        // read the response
        string serverresponse = reader.ReadToEnd();
        var end3a = DateTime.Now;
        Console.WriteLine("Finished response " + (end3a - start3));

        Console.WriteLine("Entire call took " + (end3a - start));

        //Console.WriteLine(serverresponse);
        reader.Close();
        dataStream.Close();
        response.Close();

        return serverresponse;
    }
}

And to call it I use

private void btnLogin_Click(object sender, EventArgs e)
{
    // string postdata;

    if (txtUsername.Text.Length < 3 || txtPassword.Text.Length < 3)
    {
        MessageBox.Show("Missing your username or password.");
    }
    else
    {
        string postdata = "username=" + txtUsername.Text +
                          "&password=" + txtPassword.Text;

        httppostdata myPost = new httppostdata();
        string response = myPost.senddata("http://www.domainname.com/scriptname.php", postdata);
        MessageBox.Show(response);
    }
}

解决方案

Make sure you explicitly set the proxy property of the WebRequest to null or it will try to autodetect the proxy settings which can take some time.

这篇关于为什么使用 WebRequest 发送 post 数据需要这么长时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆