I need to send a POST request with 4 parameters (login, password, city, XML) in java

Asked

Viewed 557 times

0

Good morning, I need to send these four parameters to the server to issue a note, the note data will be passed by XML, which is already all correct, more as I do this request POST via java sending these data?inserir a descrição da imagem aqui

This is the data I have to send as shown in the manual.

2 answers

1

After researching a lot I found my solution and here.

Path path path = Paths.get("/Users/Alexandre/notanvio.xml");

    HttpClient client = HttpClientBuilder.create().build();

    HttpPost post = new HttpPost(URL);
    FileBody fileBody = new FileBody(path.toFile(), ContentType.DEFAULT_BINARY);
    StringBody stringBody1 = new StringBody("15425039000128", ContentType.MULTIPART_FORM_DATA);
    StringBody stringBody2 = new StringBody("154250", ContentType.MULTIPART_FORM_DATA);
    StringBody stringBody3 = new StringBody("7513", ContentType.MULTIPART_FORM_DATA);
    // 
    MultipartEntityBuilder builder = MultipartEntityBuilder.create();
    builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

    builder.addPart("login", stringBody1);
    builder.addPart("senha", stringBody2);
    builder.addPart("cidade", stringBody3);
    builder.addPart("f1", fileBody);
    HttpEntity entity = builder.build();

    //
    post.setEntity(entity);
    HttpResponse response = client.execute(post);

1

I recommend using Apache Httpclient. It’s faster and easier to implement.

PostMethod post = new PostMethod("http://www.seusite.com/");
NameValuePair[] data = {
    new NameValuePair("login", "sss"),
    new NameValuePair("senha", "aaaa")
    new NameValuePair("cidade", "xxxx")
    new NameValuePair("fi", yourFILE)
};
post.setRequestBody(data);
// Execute o método e lida com todas as respostas de erro.
...
InputStream in = post.getResponseBodyAsStream();
// lida com resposta.
  • Hi Luiz, so it didn’t work, this Namevaluepair class doesn’t have in the library I downloaded, and the Basicnamevaluepair class I found does not accept File as second parameter.

Browser other questions tagged

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