Consuming Web Service with Login and Password

Asked

Viewed 4,669 times

1

I received these guidelines to access and consume the list methodProducts of a Webservice:

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

The client must have an operator registered on the system, with "web-service" access permission and a specific password for the access medium; the authentication mechanism uses the HTTP basic Authentication standard.

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

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

Then they must be encrypted in Base 64: d3N0ZXN0ZTp3c3Rlc3RlMzMz

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

The login runs throughout the entire 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";

Request example 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 Login and Password access and 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 appears requesting the login and password and after informing the Login and Password returns me the messages below and I’m not sure if I’m doing it right, or else there is some problem with this login and password provided by the Customer?

inserir a descrição da imagem aqui

This is the WSDL:

<!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.1-hudson-28-. -->
<!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.1-hudson-28-. -->
<definitions xmlns: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 "list of products"

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

I implemented the suggestion of Rovann Linhalis , but maybe I keep doing something wrong in the implementation because error occurs:

inserir a descrição da imagem aqui

  • 1

    it seems to me not a web-service SOAP, search for consuming WS with HTTP Post and this authentication content, you send in the request header

3 answers

3

After a few adjustments the solution was 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 to Rovann Linhalis .

  • what a beauty, I’m glad to have helped, congratulations

2

In my case it worked with a small 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 must contain at the end the string "? wsdl" (@"https://treina.ssspc.org.br/ssspc/remoting/ws/consulta/consultaWebService?wsdl").

2


I believe you have to wear one 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 encrypted login+password.

Data the content sent in the request.

url, the Ws address

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

inserir a descrição da imagem aqui

  • Hello @Rovann Linhalis thank you for replying to my post.

  • this error occurs when I post: "The specified value has invalid HTTP header characters. r nName of parameter: name". I don’t have much experience with webservice, I did a search as you guided but as the subject is broad I could not find a quick solution to this error and the worst is that at the moment I need with a certain urgency because the customer is waiting for a return. Thank you in advance!

  • I will edit the post and add more details about the error.

  • I believe it was just the : that should not be there, rsrs sorry. in my code was token, I changed as I saw in your image and went unnoticed

  • Yes, that’s right I’ll run some tests here and I’ll get back to you. Thank you!!

  • The must return in this row var = (HttpWebResponse)request.GetResponse(); ? Additional information error occurs: Remote server returned an error: (401) Unauthorized

  • will return the answer from the webservice, which will then be passed to responseString where you probably have an xml. What is happening is authorization error, possibly something wrong still in the header issue.

  • I saw your print of the consultation with Postman and I made a request also see: <?xml version='1.0' encoding='UTF-8'?>&#xA;<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">&#xA; <S:Body>&#xA; <S:Fault xmlns:ns4="http://www.w3.org/2003/05/soap-envelope">&#xA; <faultcode>S:Client</faultcode>&#xA; <faultstring>Couldn't create SOAP message due to exception: XML reader error: com.ctc.wstx.exc.WstxEOFException: Unexpected EOF in prolog&#xA; at [row,col {unknown-source}]: [1,0]</faultstring>&#xA; </S:Fault>&#xA; </S:Body>&#xA;</S:Envelope>

  • sure that user / password are ok ? I’m searching something here but haven’t found anything yet. They provide a wsdl for what I researched here, so I think it’s a SOAP yes, but, still researching

  • Thanks for the return @ Rovann Linhalis! , this user and password was sent by the SPC commercial where the password must be sent encrypted and according to the manual it must be encrypted in Base64 and also indicates this site (http://ostermiller.org/calc/encode.html) in this format: wsteste:wsteste333 example: d3N0ZXN0ZTp3c3Rlc3RlMzMz with : between login and password and in Postman as are 2 fields (Login and password) is generated another password other than the one I posted as example, tá complicado rsrs

  • I did so in Postman, added the header manually

Show 7 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.