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”
<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
<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
<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