如何使用 C# 中的 HttpWebRequest 类发送文本
我在 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”文件...
其中 writeup 字符串将使用“POST”方法(而不是“Get”方法)发布到 php 文件...
所以我可以通过变量 $_POST['writeup'] 接受 PHP 中的文本
这样我就可以根据需要处理文本...
更多问题... 最好每分钟打开和关闭 httprequest...或者在互联网连接可用时始终保持打开状态...
I have this function in C# witch is called via a timer every 1 minute...
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 = "";
}
this code is written to do the next:
check if there's a connection to the site...
if there's a one, then... send the text from the 'writeup' string variable to the 'save.php' file stored in the root of the site...
where the writeup string will be posted to the php file using 'POST' method (instead of 'Get' method)...
so I can accept the text in PHP via the variable $_POST['writeup']
so i can then process the text as I want...
more questions...
witch better open and close the httprequest every minute... or keep it open all the time the internet connection is available...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)