我目前正在创建一个C#应用程序,以连接到php / MySQL在线系统。应用程序需要将后期数据发送到脚本并获得响应。
当我发送以下数据时
username=test&password=test
我收到以下回复......
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
正如您所看到的,实际将数据发送到脚本需要6秒钟,我已经做了进一步测试,从telnet发送数据并将发布数据从本地文件发送到网址,他们甚至不需要一秒钟所以这不是网站上托管脚本的问题。
为什么在两个简单字符串时传输数据需要6秒钟?
我使用自定义类来发送数据
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;
}
}
并且我称之为
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);
}
}
答案 0 :(得分:4)
确保将WebRequest的代理属性显式设置为null,否则它将尝试自动检测可能需要一些时间的代理设置。
答案 1 :(得分:0)
有可能因为,在你的测试中,你只调用一次,你看到的延迟是C#代码被JIT编译。
更好的测试是两次调用,并从第一次丢弃时间,看看它们是否更好。
更好的测试是放弃第一组时间,然后多次运行并取平均值,但对于非常松散的“指示性”视图,这可能不是必需的。
顺便说一下,对于这种时间安排,你最好使用System.Diagnostics.Stopwatch
类而不是System.DateTime
。
<强> [编辑] 强> 另外,注意到Mant101关于代理的建议,如果设置无代理无法解决问题,您可能希望设置Fiddler并将您的请求设置为使用Fiddler作为其代理。这将允许您拦截实际的http调用,以便您可以从框架外部更好地细分http调用时间。