在VS 2013 for Web中,需要在按钮单击时将URL发送到Web服务。 [英] In VS 2013 for Web, need to send a URL on button click to a web service.
问题描述
我在远程服务器上有一个web服务,当使用Web浏览器将此URL发送到远程服务器时,它会将硬件I / O端口的状态更改为ON: http://agratek.dyndns ?。组织:8080 / web服务OP = Actuate的&安培;节点ID = 2及端口ID = 0&安培;值= ON 的。我需要在ASP.NET网页上添加一个按钮来执行相同的操作。
我在Visual Studio for Web 2013 .aspx webform文件中的代码创建了按钮:
< asp:Button ID =butValveONrunat =serverOnClick =butValveON_ClickText =Valve ON/>
这在代码隐藏.cs文件中处理按钮点击事件:
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:
protected void butValveON_Click(object sender, EventArgs e)
{
//Send the URL to remote server, wait for and capture response
}
我想等待并捕获webservice对命令的OK响应。有人可以建议一种方法或更好,一个代码片段来做到这一点吗?如果您想尝试,网络服务现在正在运行..它将返回[确定]
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" ]
推荐答案
使用WebClient的示例
sample using WebClient
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();
我提出了这个有效的解决方案。该按钮在.aspx文件中定义:
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" />
此解决方案会刷新整个页面,但它始终有效。应该为此解决方案添加一些错误和无响应检查。当WebService以OK响应时,将更新TextBox。
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;
}
}
这篇关于在VS 2013 for Web中,需要在按钮单击时将URL发送到Web服务。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!