ASP.NET Read web service data using C#

Simple web service

String errorString = String.Empty;String userId = frmUserName.Text.Trim();String password = frmPassword.Text.Trim();String status = String.Empty;String message = String.Empty;try{    XmlDocument xmlStream = new XmlDocument();    try    {        {            xmlStream.Load(String.Format("http://webservice.com:8080/example/index.jsp?username={0}&password={1}", userId, password));            status = xmlStream.SelectSingleNode("authentication/status").InnerText;            message = xmlStream.SelectSingleNode("authentication/message").InnerText;            // Check to see if an error occured)            if (status != "0")            {                errorString = message;            }        }    }    catch (Exception ex)    {        this.HandleException(ex);        errorString = "Error Validation credentials, please try again later or contact your administrator";    }}catch (Exception ex){    this.HandleException(ex);    errorString = String.Format("* There was a problem validating your user information. <BR /><BR {0}/>", ex.Message);}

HTTPS + different approach for reading data because of namespace in the root node

XML

<ArrayOfQuery_ValidateLoginReturn xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://webservice.com/WebService_SampleValidation">    <Query_ValidateLoginReturn>        <Success>true</Success>         <Message />         <Records>            <Query_ValidateLoginRow>                <Param_IsValid>Authenticated</Param_IsValid>             </Query_ValidateLoginRow>        </Records>  </Query_ValidateLoginReturn></ArrayOfQuery_ValidateLoginReturn>

C#

string errorString = string.Empty;string userId = frmUserName.Text.Trim();string password = frmPassword.Text.Trim();string status = string.Empty;string message = string.Empty;bool serviceWorkedProperly = false;try{    XmlDocument xmlStream = new XmlDocument();    XElement root;    string WEBSERVICE = string.Format("https://webservice.com/WebService_SimpleValidation.asmx/Query_ValidateLogin?Param_Username={0}&Param_Password={1}", userId, password);    try    {        {            xmlStream.Load(WEBSERVICE);            root = XElement.Load(WEBSERVICE);            XmlNamespaceManager ns = new XmlNamespaceManager(xmlStream.NameTable);            ns.AddNamespace("example", root.Name.NamespaceName);            status = xmlStream.SelectSingleNode("//example:Param_IsValid", ns).InnerText;            serviceWorkedProperly = Convert.ToBoolean(xmlStream.SelectSingleNode("//example:Success", ns).InnerText);            // Check to see if an error occured)            if (serviceWorkedProperly)            {                if (status == GetResponse(Responses.CREDENTIALS_NOT_FOUND))                {                    errorString = GetResponse(Responses.CREDENTIALS_NOT_FOUND.ToString());                }                else if (status == GetResponse(Responses.USERNAME_NOT_FOUND))                {                    errorString = GetResponse(Responses.USERNAME_NOT_FOUND.ToString());                }                else if (status == GetResponse(Responses.PASSWORD_IS_INCORRECT))                {                    errorString = GetResponse(Responses.PASSWORD_IS_INCORRECT.ToString());                }                else if (status == GetResponse(Responses.EXPIRED_OR_INACTIVE))                {                    errorString = GetResponse(Responses.EXPIRED_OR_INACTIVE.ToString());                }            }            else            {                errorString = GetResponse(Responses.VALIDATION_ERROR.ToString());            }        }        //#endif    }    catch (Exception ex)    {        this.HandleException(ex);        errorString = GetResponse(Responses.VALIDATION_ERROR.ToString());    }}catch (Exception ex){    this.HandleException(ex);    errorString = String.Format("{0}<BR /><BR />{1}", GetResponse(Responses.VALIDATION_ERROR.ToString()), ex.Message);}

string GetResponse(Enum value){    if (value == null)    {        throw new ArgumentNullException("value");    }    Type type = value.GetType();    System.Reflection.FieldInfo fieldInfo = type.GetField(Enum.GetName(type, value));    var descriptionAttribute =        (DescriptionAttribute)Attribute.GetCustomAttribute(                                   fieldInfo, typeof(DescriptionAttribute));    if (descriptionAttribute != null)    {        return descriptionAttribute.Description;    }    else    {        return string.Empty;    }    //return value.ToString();}string GetResponse(string value){    return GetLocalResourceObject(value).ToString();}

Leave a Reply

Your email address will not be published. Required fields are marked *