1. What is webserver?
Daily life often use to webserver, registration, will receive a verification code, buy things, will receive text messages, if, a company website and B Company Cooperation, then a company registered to the user can directly push to B site, how to do it? is the database information directly to it?
Of course not, is this a safe way to do this? General a site will provide a webserver interface to the B site call.
What exactly is webserver, webserver is used for different system data communication.
2. How do we use the webserver?
2.1 Creating webserver
Project-Right-Add New item-Select Web Service
After that, run, click HelloWorld to come in and copy this address
2.2 Calling Webserver
2.2.1 Reference-Add Service reference-advanced-Add Web reference [the address just copied, paste in this]-add reference
2.2.2 Then create a new Web form. cs to instantiate an object, calling its method
1 protected void Page_Load (object sender, EventArgs e) 2 {3 //1. Create a WebServices object 4 localhost. WebService1 ws = new localhost. WebService1 (); 5 Response.Write (ws. HelloWorld ()); 6 }
View the result of the call in the browser:
But: the above two companies to make interface calls, there are unsafe factors, interface public, unsafe, then how can we avoid this problem at this time?
Solution: We can provide an encrypted interface in a company, and then this docking password only tell company B, if it is the correct password, let it call the interface, if the password is incorrect, access is denied. [. Asmx.cs]
1 namespace baidu20160707 2 {3//1.1 Write a class, the way to call it through the class, if the password passed in the same, let it access, if different, deny it access to 4 public class Mysoapheade R:soapheader 5 {6 public string pwd {get; set;} 7 public bool Check (string pwd) 8 {9 if (pwd = = "123456") ten {one return true;12}13 else14 {false;16}17}18}19//<summary>20 To illustrate///</summary>22 [WebService (Namespace = "http://tempuri.org/")]23 [webservicebinding (ConformsTo = wsiprofiles.basicprofile1_1)]24 [System.ComponentModel.ToolboxItem (FALSE)]25//To allow the use of ASP. NET AJAX to invoke this Web service from the script Please uncomment the line. //[System.web.script.services.scriptservice]27 public class WebService1:System.Web.Services.WebService28 {29//1.2 instantiating an object, calling it through an object method of public Mysoapheader header;31 [webmethod]32//1.3 via Mysoaphea A Heade in der.The R method does verify that the [SoapHeader ("header")]34 public string HelloWorld () 35 {36//1.437 if (Header.check (HEADER.PWD)), "Hello World",}41 Else4 2 {"You do not have permission to call"; 44}45}46}47}
, C1
1 protected void Page_Load (object sender, EventArgs e) 2 {3 //1. Create a WebServices object 4 localhost. WebService1 ws = new localhost. WebService1 (); 5 //Create an object of Mysoapheader 6 localhost. Mysoapheader Header = new localhost. Mysoapheader (); 7 header.pwd = "123456"; 8 ws. Mysoapheadervalue = header; 9 //2. Call method by Object Response.Write (ws. HelloWorld ());
Operation Result:
3. So here we are going to do this effect:
3.1. First, add the service reference-advanced-Add Web reference-put http://www.webxml.com.cn/WebServices/WeatherWebService.asmx? This interface address is copied above, add reference
3.2. Create a new Web form, HTML code
1 <body> 2 <form id= "Form1" runat= "Server" > 3 <div> 4 <asp:label id= "Label1" runat= " Server "text=" City: "></asp:Label> 5 <asp:textbox id=" txtgetweatherbycityname "runat=" Server ">< /asp:textbox> 6 <asp:button id= "Button1" runat= "server" text= "Query Weather" onclick= "Button1_Click"/> 7 </div> 8 <div id= "D1" ><%=sweatherHtml%></div> 9 </form>10 </body>
Background page. CS Note: [using StringBuilder to introduce namespaces using System.Text;]
1 protected void Page_Load (object sender, EventArgs e) 2 {3 4} 5//4. Write a string Bind to front end 6 public string sweatherhtml = ""; 7 protected void Button1_Click (object sender, EventArgs e) 8 {9//1. Create an Object ten CN.C Om.webxml.www.WeatherWebService ws = new Cn.com.webxml.www.WeatherWebService (); 11//3. Get the contents of the text box 12 String txtcity = txtgetweatherbycityname.text;13//2. Call its Getweatherbycityname method because it returns an array of strings, so to define a string array variable Collect it. String [] aweather= ws.getweatherbycityname (txtcity); 15//5. Write a string piece together and get a table of the Strin Gbuilder sb = new StringBuilder (); Append ("<table>"); Append (String. Format ("<tr><td> area:</td><td>{0}</td>", aweather[0]. ToString ())); Append (String. Format ("<tr><td> City:</td><td>{0}</td>", Aweather[1]. ToString ()));Append (String. Format ("<tr><td> time:</td><td>{0}</td>", aweather[4]. ToString ())); sb. Append (String. Format ("<tr><td> temperature:</td><td>{0}</td>", aweather[5]. ToString ())); Append (String. Format ("<tr><td> weather:</td><td>{0}</td>", aweather[6]. ToString ())); (Aweather[8]. ToString () = = Aweather[9]. ToString ()). Append (String. Format (@ "<tr><td> weather </td><td></td></tr>", AWEATHER[8]. ToString ()));}27 else28 {sb. Append (String. Format (@ "<tr><td> weather </td><td> to </td></tr>", aweather[8]. ToString (), aweather[9]. ToString ()));}31 sb. Append (String. Format ("<tr><td> Today's weather fact: </td><td>{0}</tD> ", aweather[10]. ToString ())); Append (String. Format ("<tr><td> hint:</td><td>{0}</td>", aweather[11]. ToString ())); Append ("</table>"); sweatherhtml = sb. ToString (); 35}36}
The result of the operation is the above results, in addition to CSS style you can set your own preferences.
Webserver "Real-time query of weather conditions on the day"