0

So I have this javascript function, and I want to convert it to a java function. This is the original function:

function mailExistsCheck() {
request({
    uri: "https://iag.ksmobile.net/1/cgi/is_exist",
    method: "POST",
    headers: {
        'User-Agent': "FBAndroidSDK.0.0.1", DONE
        'Accept-Language': "en_US", DONE
        'appid': "135301",
        'ver': "3.8.20",
        'sid': "17DBB0C2B30D01B590B704501A8AC054",
        'sig': "lbOfPDrq0kJNvZX8eYZH9c0Mo7o",
        'Host': "iag.ksmobile.net",
        'Content-Type': "multipart/form-data; boundary=3i2ndDfv2rTHiSisAbouNdArYfORhtTPEefj3q2f",
        'Transfer-Encoding': "chunked",
        'Accept-Encoding': "gzip"
    },
    body: `--3i2ndDfv2rTHiSisAbouNdArYfORhtTPEefj3q2f
        Content-Disposition: form-data; name="cmversion"

        38201849
        --3i2ndDfv2rTHiSisAbouNdArYfORhtTPEefj3q2f
        Content-Disposition: form-data; name="name"

        ` + address + `
        --3i2ndDfv2rTHiSisAbouNdArYfORhtTPEefj3q2f`
}, (error, response, body) => {
    if (error) {
        console.error(error);
    }
    if (body.indexOf("12018") != -1) {

        register();

    } else {
        console.error(body);
        console.error("MAIL EXISTENCE CHECK FAILED!");
        process.exit(1);
    }
});

I'm using this library http://kevinsawicki.github.io/http-request/ to create the http request, so far I have this:

public static String mailexiste(String address) {
String contentType = HttpRequest.post("https://iag.ksmobile.net/1/cgi/is_exist")
        .userAgent("FBAndroidSDK.0.0.1")
        .header("Accept-Language", "en_US")
        .header("appid", "135301")
        .header("ver", "3.8.20")
        .header("sid", "17DBB0C2B30D01B590B704501A8AC054")
        .header("sig", "lbOfPDrq0kJNvZX8eYZH9c0Mo7o")
        .header("Host", "iag.ksmobile.net")
        .contentType("multipart/form-data; boundary=3i2ndDfv2rTHiSisAbouNdArYfORhtTPEefj3q2f")
        .acceptGzipEncoding()
        .contentType(); //Gets response header


return contentType;

I think I did the headers the right way, my only issue is that I'm having problems with the body part. I don't understand most of this requests, because I'm just starting now to learn this, and I would like to do the program by myself, but I'm really stuck on this part. Also, how do I do the if's in java? Thanks in advance!

  • 3
    You're better off using [okhttp](http://square.github.io/okhttp/). Look into how to send [form data with okhttp](https://stackoverflow.com/questions/23456488/how-to-use-okhttp-to-make-a-post-request). – Ahmad Mar 22 '18 at 18:54
  • I will take a look at it @Ahmad ty – pedroribeiro98 Mar 22 '18 at 19:15
  • I thinks you can use this [method](https://github.com/kevinsawicki/http-request/blob/2d62a3e9da726942a93cf16b6e91c0187e6c0136/lib/src/main/java/com/github/kevinsawicki/http/HttpRequest.java#L3045) to set any string for body – Amir Khorsandi Mar 22 '18 at 20:46

2 Answers2

1
  • Drop the boundary - unless your server is non-HTTP-compliant, any string should do
  • Drop the Host header, it will be added by the library automatically
  • Use .part(String name,String value) to add parts in a multipart:

You should get something like:

   .contentType("multipart/form-data")
   .part("cmversion", "38201849")
   .part(name, address)
Tassos Bassoukos
  • 16,017
  • 2
  • 36
  • 40
0

if() is pretty much the same in Java as in JavaScript.

MadMax
  • 605
  • 1
  • 6
  • 19
  • I know that, I asked about the if's, in like, how can I determinate if the answer is error, etc... Also, my main issue is the body part – pedroribeiro98 Mar 22 '18 at 19:15