我在C#中有这个函数,它通过一个计时器每1分钟调用一次…
private void timer1_Tick(object sender, EventArgs e)
{
string strServer = "hhttp://www.mydomain.net/save.php";
try {
HttpWebRequest reqFP = (HttpWebRequest)HttpWebRequest.Create(strServer);
HttpWebResponse rspFP = (HttpWebResponse)reqFP.GetResponse();
if (rspFP.StatusCode == HttpStatusCode.OK) { // ther is an internet connection
//send the text stored in 'writeUp' string variable to the url via 'POST' methode
rspFP.Close(); //is good to open and close the connection every minute
}
}
catch (WebException) {
//I don't know why to use try/catch... but I just don't want any errors to be poped up...
}
writeUp = "";
}这段代码是用来做下一步的:
检查是否有到网站的连接...
如果有1,那么..。将“writeup”字符串变量中的文本发送到存储在站点根目录中的“save.php”文件...
其中writeup字符串将使用'POST‘方法(而不是'Get’方法)发布到php文件中……
因此,我可以通过变量$_POST‘’writeup‘接受PHP中的文本。
这样我就可以随心所欲地处理文本了。
更多问题...最好每分钟打开和关闭httprequest ...或者在互联网连接可用时始终保持打开状态...
发布于 2011-01-27 23:07:25
private void timer1_Tick(object sender, EventArgs e)
{
string strServer = "hhttp://www.mydomain.net/save.php";
try
{
var reqFP = (HttpWebRequest)HttpWebRequest.Create(strServer);
reqFP.Method = "POST";
reqFP.ContentType = "application/x-www-form-urlencoded";
reqFP.ContentLength = writeup.Length;
/*var rspFP = (HttpWebResponse)reqFP.GetResponse();
if (rspFP.StatusCode == HttpStatusCode.OK)
{*/
//WRITE STRING TO STREAM HERE
using (var sw = new StreamWriter(reqFP.GetRequestStream(), Encoding.ASCII))
{
sw.Write(writeup);
}
rspFP.Close(); //is good to open and close the connection every minute
/*}*/
}
catch (WebException) {
//I don't know why to use try/catch...
//but I just don't want any errors to be poped up...
}
writeUp = "";
}https://stackoverflow.com/questions/4817895
复制相似问题