Consume Web Service with Login and Password

1

I have received these guidelines to access and consume the ListSourceProducts method of a WebService:

Standard form of authentication of the Java application webservices for entities and associates

It is necessary for the client to have an operator registered in the system, with access permission "web-service" and password specific to the access medium; the authentication mechanism uses the HTTP basic authentication pattern.

The login data (operator and password) must be sent in the specific header (Authorization), encrypted in Base 64, example:

The operator name (in the example, "wsteste") and the password (in the example, "wsteste333") must be concatenated: wsteste: wsteste333

Next, they should be encrypted in Base 64: d3N0ZXN0ZTp3c3Rlc3RlMzMz

The requests must be sent containing the header, using the sequence encrypted: Authorization: Basic d3N0ZXN0ZTp3c3Rlc3RlMzMz

The login is executed in every web-service operation; therefore, every request must have the header specified above; if the login does not occur successfully, the service returns "HTTP ERROR: 401";

Sample request containing the header:

Cookie: $Version=0; JSESSIONID=48g39p406ezh_dev01; $Path=/spc
Authorization: Basic d3N0ZXN0ZTp3c3Rlc3RlMzMz
Host: localhost:8080
Content-Length: 1799
SOAPAction: ""
User-Agent: Jakarta Commons-HttpClient/3.0.1
Content-Type: text/xml;charset=UTF-8
Connection: close
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:web="http://webservice.consulta.xxxjava.spcbrasil.org/">
 <soapenv:Header/>
 <soapenv:Body>
 ...
 </soapenv:Body>
</soapenv:Envelope>

I received the Login and Password and I have some knowledge in consuming WebService based on some articles:

ASP. NET 2008 - Creating Web Services II
VB. NET - Consuming Web Services with Windows Forms

The problem is that when adding the reference to the project and the screen prompts for the login and password and after informing Login and Password, I return the messages below and I'm not sure if I'm doing it right, or there is a problem with that login and password provided by the Customer?

ThisisWSDL:

<!--PublishedbyJAX-WSRIathttp://jax-ws.dev.java.net.RI'sversionisJAX-WSRI2.2.1-hudson-28-.--><!--GeneratedbyJAX-WSRIathttp://jax-ws.dev.java.net.RI'sversionisJAX-WSRI2.2.1-hudson-28-.--><definitionsxmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://webservice.consulta.spcjava.spcbrasil.org/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://webservice.consulta.spcjava.spcbrasil.org/" name="consultaWebService">
<types>...</types>
<message name="listarProdutos"/>
<message name="listarProdutosResponse">
<part name="produtos" element="tns:produtos"/>
</message>
<message name="consultar">...</message>
<message name="consultarResponse">...</message>
<message name="detalharProduto">...</message>
<message name="detalharProdutoResponse">...</message>
<message name="consultaComplementar">...</message>
<message name="consultaComplementarResponse">...</message>
<message name="consultaScore">...</message>
<message name="consultaScoreResponse">...</message>
<portType name="consultaWebService">...</portType>
<binding name="consultaWebServicePortBinding" type="tns:consultaWebService">...</binding>
<service name="consultaWebService">...</service>
</definitions>
  

Operation "listProducts"

Description: Returns the products available for consultation. Home Rules of use:
1. If there are no products available to the operator, the operation does not return given. Home Input Parameters: not applicable

I've implemented the Rovann Linhalis tip, but maybe I still do something wrong in the implementation because it occurs error:

    
asked by anonymous 05.07.2017 / 13:32

3 answers

2

I believe you have to use HttpWebRequest :

  public static string HttpPost(string url, string postData, string token)
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

        byte[] data = Encoding.ASCII.GetBytes(postData);
        request.Headers["authorization"] = "basic: "+ token;
        request.Method = "POST";
        //request.Accept = "application/json"; //?
        request.ContentType = "text/xml; charset=utf-8"; //?
        request.ContentLength = data.Length;

        using (var stream = request.GetRequestStream())
        {
            stream.Write(data, 0, data.Length);
        }

        var response = (HttpWebResponse)request.GetResponse();

        var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();

        return responseString;
    }

where:

token would be your login + encrypted password.

postData the content sent in the request.

url, the address of ws

Try to put the test documentation there, here in mine with the data that I could read from the image, it looks like this:

    
05.07.2017 / 13:56
3

After some adjustments the solution looks like this:

[HttpPost]
        public ActionResult Index(string url, string login_senha)
        {
            var _url = @"https://treina.ssspc.org.br/ssspc/remoting/ws/consulta/consultaWebService";
            var _HttpPost = HttpPost(_url, login_senha);
            return View();
        }

        public static string HttpPost(string url, string login_senha)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Headers.Add("Authorization", "Basic " + login_senha);
            request.Method = "GET"; 
            request.ContentType = "text/xml; charset=utf-8";
            var response = (HttpWebResponse)request.GetResponse();
            var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
            return responseString;
        }

Credits at Rovann Linhalis .

    
11.07.2017 / 15:08
2

In my case it worked with a minor change:

public static string HttpPost (string url, string postData, string token)     {         HttpWebRequest request = (HttpWebRequest) WebRequest.Create (url);

    byte[] data = Encoding.ASCII.GetBytes(postData);
    //request.Headers["authorization"] = "basic: "+ token;
    request.Headers.Add("Authorization", "Basic " + token);
    request.Method = "POST";
    //request.Accept = "application/json"; //?
    request.ContentType = "text/xml; charset=utf-8"; //?
    request.ContentLength = data.Length;

    using (var stream = request.GetRequestStream())
    {
        stream.Write(data, 0, data.Length);
    }

    var response = (HttpWebResponse)request.GetResponse();

    var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();

    return responseString;
}

Note: The url should end with the string "? wsdl" (@ " link ").

    
10.10.2017 / 20:39