0

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...

2
  • I only see a single question here - "should I open and close". What other questions are there? Commented Jan 28, 2011 at 4:58
  • I would get rid of the try/catch. The best way to Commented Jan 28, 2011 at 4:59

1 Answer 1

1
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 = "";
}
Sign up to request clarification or add additional context in comments.

6 Comments

I've tried it on localhost... but its give an error: This operation cannot be performed after the request has been submitted.
@wisam-mo please try now. I think the problem is because you are calling GetResponse() before the writing to stream.
thanks TheVillageIdiot... its done, but theres still a tiny problem... I still can't receive the variable in save.php page... no values in $_POST were found...
with that code no data is sent via method POST... what can I do!!?? can any body help...
Problem... Can't receive data in PHP file... cuz no data was sent via the sent method... can you continue helping me... or do I need to post it in a new question...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.