Click here to Skip to main content
15,895,462 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a webservice on a remote server that changes the state of a hardware I/O port to 'ON' when this URL is sent to the remote server using a web browser: http://agratek.dyndns.org:8080/webservice?Op=Actuate&NodeID=2&PortID=0&Value=ON. I need to add a button to an ASP.NET web page that does the same thing.

My code in the Visual Studio for Web 2013 .aspx webform file creates the button:

<asp:Button ID="butValveON" runat="server" OnClick="butValveON_Click" Text="Valve ON" />

And this in the code-behind .cs file handles the button click event:

C#
protected void butValveON_Click(object sender, EventArgs e)
{
    //Send the URL to remote server, wait for and capture response
}


I would like to wait for and capture the webservice's "OK" response to the command. Can someone suggest an approach or better, a code fragment to do this please? The webservice is running now if you would like to try it..it will return [ "OK" ]

Posted
Updated 4-Dec-14 12:32pm
v2
Comments
ZurdoDev 4-Dec-14 21:20pm    
1. I highly recommend removing the actual link. Not a good idea to publish that on the internet.
2. What is the webservice expecting? XML? Http post? You need to know what it expects.
Gordon Minns 5-Dec-14 0:28am    
Hi Ryan... Thanks for responding...

The webservice server is only a test server and no harm will be done if people try to access it. But you are right... who knows what evils lurk out there...

You wrote: What is the webservice expecting? XML? Http post? You need to know what it expects.

The webservice is written to operate on the arguments sent in the URL I mentioned. We use JSON to send data to the requesting web page, but the URL as written effects the changes in the I/O ports.
ZurdoDev 5-Dec-14 7:29am    
Then Solution 1 from DamithSL will likely work.

sample using WebClient
C#
string url ="http://agratek.dyndns.org:8080/webservice?Op=Actuate&NodeID=2&PortID=0&Value=ON";
System.Net.WebClient myWebClient = new System.Net.WebClient();
Stream myStream = myWebClient.OpenRead(url);
StreamReader sr = new StreamReader(myStream);
string returnData = sr.ReadToEnd(); // check for OK
myStream.Close();
 
Share this answer
 
v3
Comments
Gordon Minns 7-Dec-14 16:09pm    
Solution 1 give the error message: No overload for method 'OpenRead' takes 0 arguments. Thanks for your answer.
DamithSL 7-Dec-14 23:10pm    
Oh sorry , need to give the url,how I missed that!
I came up with this solution that works. The button is defined in the .aspx file:
<asp:button id="butValve0_ON" runat="server" onclick="butValve0_ON_Click" text="Valve 0 ON" width="105px" height="25px" xmlns:asp="#unknown" />


This solution causes the whole page to be refreshed, but it always works. One should add some error and no-response checking to this solution. A TextBox is updated when the WebService responds with "OK".

<pre>        protected void butValve0_ON_Click(object sender, EventArgs e)
        {
            //Send the URL to remote server, wait for and capture response
            Uri myUri = new Uri("http://agratek.dyndns.org:8080/webservice?Op=Actuate&NodeID=2&PortID=0&Value=ON");
            System.Net.WebClient myWebClient = new System.Net.WebClient();
            Stream myStream = myWebClient.OpenRead(myUri);
            StreamReader sr = new StreamReader(myStream);
            string returnData = sr.ReadToEnd(); // check for OK
            tbValve0State.Text = returnData;
            myStream.Close();

            if ( returnData.Contains("OK") )
            {
                tbValve0State.Text = "ON";
                tbValve0State.BackColor = System.Drawing.Color.LightGreen;
            }

        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900