本文介绍了C#Webservice HTML获取请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Web服务的新手,但基本上我创建了一个从数据库中读取的Web服务,然后将值返回给客户端。以下是我的网络服务示例。



I am new to web services but basically i have created a webservice that reads from a db and then returns a value back to the client. Here is an example of my webservice.

namespace TestWebService1
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class Service1 : System.Web.Services.WebService
    {

        [WebMethod]
        [ScriptMethod(UseHttpGet=true)]
        public string getID()
        {
            SqlConnection cs = new SqlConnection(@"Server=130-ASQL1-B-001.OSCD.LOCAL;Initial Catalog=AIM;User ID=user;Password=pass");

            SqlCommand cmd = new SqlCommand("select id from NIM where stationName='" + System.IO.Path.GetDirectoryName(HttpContext.Current.Request.ServerVariables["REMOTE_USER"]) + "' and availability = 'reachable'", cs);

            cmd.CommandType = CommandType.Text;

            cmd.Connection = cs;

            cs.Open();

            SqlDataReader dr = cmd.ExecuteReader();


            dr.Read();

                return dr["id"].ToString();

                dr.Close();
                cs.Close();


        }
    }
}





现在好了我会喜欢从html页面调用此Web服务





Ok now I would like to call this web service from an html page

<div style="display: hidden;">
<form action='http://localhost:52618/Service1.asmx/HelloWorld?ID='

method="get" target="_self">
<table>
  <tr>
    <td>GET ID</td>
    <td>
    <input class="frmInput" type="text" size="30" name="id">
    </td>
  </tr>
  <tr>
    <td></td>
    <td align="right">
     <input type="submit" id="submitButton" value="Submit" class="button">
     </td>
  </tr>
</table>
</form>
<script language="javascript">
        document.getElementById("submitButton").click();
    </script>
</div>





出现的肥皂信息如下。





the soap message that appears is below.

<?xml version="1.0" encoding="UTF-8"?>
<string xmlns="http://tempuri.org/">0</string>





到目前为止,我能够很好地调用Web服务,但是它会调用Web服务并在浏览器的网页上显示soap消息。我想以某种方式在URL中显示我的soap消息的值,如http:// localhost:52618 / Service1.asmx / HelloWorld?ID = 42或类似的东西。我知道这可以从我见过的例子中完成,但我显然没有在网络服务或客户端做正确的事情。此外,我不确定这是从HTML页面调用Web服务的最佳方式,所以如果有人对此有任何了解,那也很好。我不得不使用javascript,但它只是使用HTML很好。感谢任何帮助!



So far i am able to call the web service fine but what happens is it calls the web service and display the soap message on the webpage within the browser. I would like to somehow show the value of my soap message in the url like http://localhost:52618/Service1.asmx/HelloWorld?ID=42 or something like that. I know this can be done from the examples i've seen but i'm obviously not doing something right on either the web service or client side. Also i'm not exactly sure that this is the best way to call a web service from an html page so if anyone has any knowledge on that that would be great as well. I am having to use javascript but it would be nice just use html. Any Help is Appreciated!

推荐答案


这篇关于C#Webservice HTML获取请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 15:07