<RESOLVIDO> Problema ao consumir webservice

Boa tarde meus caros…

Estou tentando consumir o webservice “service.asmx” em .net.
A conexão é feita porém quando chega no “if” retorna a mensagem “bad request”.

public boolean Login()
{
	  String resultado = "";
      HttpURLConnection urlConnection = null;
      
      try {
    	URL _url = new URL("http://www.xxx.com.br/ws/Service.asmx");
    	              
        urlConnection = (HttpURLConnection) _url.openConnection();
        urlConnection.setDoOutput(true);
        urlConnection.setRequestMethod("POST");
        urlConnection.setRequestProperty("Content-Type", 
                "text/xml; charset=utf-8");
        urlConnection.setRequestProperty("SOAPAction", 
                "http://tempuri.org/Login");
                  
                    
       String request = " <?xml version=\"1.0\" encoding=\"utf-8\"?>" +
        "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
        "<soap:Body>" +
            "<Login xmlns=\"http://tempuri.org/\">" +
              "<p_Usuario>teste</p_Usuario>" +
              "<p_Senha>123</p_Senha>" +
            "</Login>" +
          "</soap:Body>" +
        "</soap:Envelope>";

        OutputStream out = urlConnection.getOutputStream();
        out.write(request.getBytes());
        
        // Verifica se a resposta está ok antes de solicitar os dados

if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
InputStream in = new BufferedInputStream(
urlConnection.getInputStream());

              BufferedReader reader = new BufferedReader(
                      new InputStreamReader(in,
                              "UTF-8"), TAM_MAX_BUFFER);
              
              StringBuilder builder = new StringBuilder();
              
              for (String line = null ; (line = reader.readLine())!= null;) {
                  builder.append(line).append("\n");
              }
              
              resultado = builder.toString();
              
              // Retira a string <?xml version="1.0" encoding="utf-8" ?> 
              // <string xmlns="http://tempuri.org/"> e a tag </GetEstadosResult> 
              // para obter o resultado em Json, já que o webservice está
              // retornando uma string
              Integer firstTagString = resultado.indexOf("<LoginResult");
              Integer posXml = resultado.indexOf(">", firstTagString);
              Integer posTagString = resultado.indexOf("</LoginResult>");
              resultado = resultado.substring(posXml + 1, posTagString + 1);
        }
        else{
        	String d = urlConnection.getResponseMessage();
            Log.e("WebService", 
                    "ResponseCode: " + urlConnection.getResponseMessage());
        }

      }
      catch(IOException e){
          Log.e("WebService", e.toString());
      }
      finally {
          if (urlConnection != null) {
              urlConnection.disconnect();
          }
     }
      
      return true;
	
}

abaixo as diretrizes do webservice

SOAP 1.1

The following is a sample SOAP 1.1 request and response. The placeholders shown need to be replaced with actual values.

POST /ws/service.asmx HTTP/1.1
Host: www.xxx.com.br
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: “http://tempuri.org/Login

<?xml version="1.0" encoding="utf-8"?>

<soap:Envelope xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance” xmlns:xsd=“http://www.w3.org/2001/XMLSchema” xmlns:soap=“http://schemas.xmlsoap.org/soap/envelope/”>
soap:Body

<p_Usuario>string</p_Usuario>
<p_Senha>string</p_Senha>

</soap:Body>
</soap:Envelope>
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>

<soap:Envelope xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance” xmlns:xsd=“http://www.w3.org/2001/XMLSchema” xmlns:soap=“http://schemas.xmlsoap.org/soap/envelope/”>
soap:Body

string

</soap:Body>
</soap:Envelope>
SOAP 1.2

The following is a sample SOAP 1.2 request and response. The placeholders shown need to be replaced with actual values.

POST /ws/service.asmx HTTP/1.1
Host: www.xxx.com.br
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>

<soap12:Envelope xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance” xmlns:xsd=“http://www.w3.org/2001/XMLSchema” xmlns:soap12=“http://www.w3.org/2003/05/soap-envelope”>
soap12:Body

<p_Usuario>string</p_Usuario>
<p_Senha>string</p_Senha>

</soap12:Body>
</soap12:Envelope>
HTTP/1.1 200 OK
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance” xmlns:xsd=“http://www.w3.org/2001/XMLSchema” xmlns:soap12=“http://www.w3.org/2003/05/soap-envelope”>
soap12:Body

string

</soap12:Body>
</soap12:Envelope>

Desde já agradeço

Bom dia Willian.

Primeiro uma dica: use a tag Code para os códigos que você usa aqui, seu texto ficará bem melhor formatado e fácil de ler, e possivelmente mais pessoas tentarão ajudar.

Outra dica seria utilizar http Client para fazer a requisição ao webservice. Segue como eu faço aqui:

protected String enviaDados(String strXML) {
	String strURL = "http://www.xxx.com.br/ws/Service.asmx";
        
        PostMethod post = new PostMethod(strURL);
        post.setRequestEntity(new InputStreamRequestEntity(new ByteArrayInputStream(strXML.getBytes()), strXML.length()));
        post.setRequestHeader("Content-type", "text/xml; charset=utf-8");
        post.setRequestHeader("SOAPAction", "http://tempuri.org/Login");
        
        HttpClient httpclient = new HttpClient();
        
        String xmlRetorno = "";
        
        try {
            int result = httpclient.executeMethod(post);
                       
            xmlRetorno = post.getResponseBodyAsString();
            
        } catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
            post.releaseConnection();
        }
        
        return xmlRetorno;
	}

Acredito que dessa forma seja bem mais simples.

Teste ai e nos diga se deu certo.

Show de bola, agradeço a atenção, abraços…