Webservice is called for authentication and webservice authentication.
My colleague said that the authentication window will pop up when webservice is called, and 401 will be returned for direct calls because authentication is set during site deployment (the account name is configured by myself ). Therefore, the authentication credential must be added during the call.
How can I obtain the creden for identity authentication? You can use the NetworkCredential (The namespace is System. Net) provided by Microsoft. The Code is as follows.
Using System; using System. net; using webserviceSync. helloWebservice; namespace webserviceSync {public partial class WebForm1: System. web. UI. page {protected void Page_Load (object sender, EventArgs e) {MySoapHeader my = new MySoapHeader (); my. userName = ""; Response. write (webserviceClient. helloWorld (my ));} /// <summary> /// obtain the network credential /// </summary> /// <returns> </returns> public static NetworkCredential GetCredential () {// return new NetworkCredential ("Administrator", "888888", "192.168.2.58"); // return (NetworkCredential) CredentialCache directly. defaultCredentials; // set local creden }/// <summary> /// verify whether the creden pass. /// </summary> /// <returns> </returns> public static bool IsVerifySucess () {string url =" http://192.168.2.58:7777/WebService.asmx?wsdl "; WebResponse webResponse = null; try {WebRequest webRequest = WebRequest. create (url); webRequest. credentials = GetCredential (); webRequest. timeout = 15000; webResponse = webRequest. getResponse ();} catch (Exception ex) {throw ex;} return true ;} /// <summary> /// attributes /// </summary> private static WebServiceSoapClient _ webserviceClient = null; /// <summary> /// webservice // </summary> public static WebServiceSoapClient webserviceClient {get {if (_ webserviceClient = null) {bool isSucess = IsVerifySucess (); if (isSucess = true) {_ webserviceClient = new WebServiceSoapClient (); _ webserviceClient. clientCredentials. windows. clientCredential = GetCredential (); // This property ........}} return _ webserviceClient ;}}}}
How to Set creden locally refer to http://jingyan.baidu.com/article/a3aad71aaa32eeb1fb0096c8.html