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
  • 3
    You're better off using okhttp. Look into how to send form data with okhttp. Commented Mar 22, 2018 at 18:54
  • I will take a look at it @Ahmad ty Commented Mar 22, 2018 at 19:15
  • I thinks you can use this method to set any string for body Commented Mar 22, 2018 at 20:46

2 Answers 2

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)
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks! I'll try when I get home again, but seems promissing! Thanks!
I did that, but I always get "application/json; charset=utf-8" as a answer... The string always return that...
Looks like you're reading the wrong parts of the string? I'd suggest you use something like curl or Postman to accurately replay the HTTP request, then try to map it to Java.
It works with javascript, so I think the http request is ok
0

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

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.