如何使用 C# 中的 HttpWebRequest 类发送文本

发布于 2024-10-14 07:09:00 字数 1065 浏览 2 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薯片软お妹 2024-10-21 07:09:00
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 = "";
}
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 = "";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文