Problema com o Android HTTP POST
Estou usando o código abaixo para executar uma solicitação HTTP POST. O PostData está na forma de uma String
Amostra de PostData:
urn: marlin: banda larga: 1-1: serviço de registro: li nkAcquisitionurn: marlin: organização: testpdc: devi ce-maker-x: clien tnemo: aa08a1: 59e a7e8cfa7a8582http: //docs.oasi s-open. org / wss / 2 004/01 / oasis-200 401-wss-wsecuri ty-utility-1.0.x sd "URI =" URL: mar lin: core: 1.0: nem o: protocol: profi le: 1 "wsu: Id = "si gid0003" nmosec: Usage = "http: // no emo.intertrust.c om / 2005/10 / security / profile" /> </ SOAP-ENV: Desenvolvido por>
Esperamos uma resposta xml / soap, em vez disso, estamos recebendo um arquivo xml como resposta. Alguém pode me dizer se o procedimento para executar um HTTP POST está correto (como no código abaixo)
Nota: O mesmo postData quando usado com cuRL para executar um POST está funcionando bem.
public byte [] sendRecv(String PostData, long postDataSize){
try{
if(!(PostData.equals("empty"))){
isPost = true;
//InputStream postDataInputStream = new ByteArrayInputStream(postData);
//BasicHttpEntity httpPostEntity = new BasicHttpEntity();
//httpPostEntity.setContent(postDataInputStream);
StringEntity httpPostEntity = new StringEntity(PostData, HTTP.UTF_8);
//httpPostEntity.setContentLength(postData.length);
//httpPostEntity.setContentType("application/x-www-form-urlencoded");
httpPost.setEntity(httpPostEntity);
httpPost.setHeader("Content-Length", new Integer(PostData.length()).toString());
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
httpPost.setHeader("Expect", "100-continue");
}
}catch(Exception e) {
e.printStackTrace();
}
try {
if(isPost == true){
response = mAndroidHttpClient.execute(httpPost);
isPost = false;
}
else {
response = mAndroidHttpClient.execute(httpGet);
}
statusCode = response.getStatusLine().getStatusCode();
if(statusCode != 200 ){
if(statusCode == 404) {
//System.out.println("error in http connection : 404");
//return ERR_HTTP_NOTFOUND
} else if(statusCode == 500){
//System.out.println("error in http connection : 500");
//return ERR_HTTP_INTERNALSEVERERROR
} else {
//System.out.println("error in http connection : error unknown");
//return ERR_HTTP_FATAL
}
}
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
for (int next = is.read(); next != ENDOFSTREAM; next = is.read()) {
bos.write(next);
}
responseBuffer = bos.toByteArray();
bos.flush();
bos.close();
}catch (IOException e) {
e.printStackTrace();
}
return responseBuffer;
}