[英]how to send text using HttpWebRequest class in C#
我在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 = "";
}
编写此代码以执行下一个步骤:
检查该站点是否存在连接...
如果有一个,那么...将文本从'writeup'字符串变量发送到存储在网站根目录中的'save.php'文件...
使用'POST'方法(而不是'Get'方法)将编写字符串发布到php文件中...
因此我可以通过变量$ _POST ['writeup']接受PHP中的文本
这样我就可以根据需要处理文本了...
更多问题...女巫最好每分钟打开和关闭httprequest ...或在互联网可用的所有时间内保持打开状态...
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 = "";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.