简体   繁体   中英

Converting javascript code to android code - how to add referrer in POST request?

I have this java script code on my website which gets executed when someone subscribes to my newsletter. It is basically nothing but post request. This is the piece of code.

function es_submit_request(url, parameters, es_widget_form) {
http_req = false;

http_req.onreadystatechange = function() {eemail_submitresult(es_widget_form)}; // Passing the form to the submit request
http_req.open('POST', url, true);
http_req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http_req.send(parameters);
}

Is is possible to call this post method from my android application code to subscribe someone to my newsletter?

I have tried this code here but it is not working.

When I debug my js code, variable values are coming as

parameters = "es_email=fgnfg@dgd.com&es_name=&es_group=&timestamp=&action=0.9901232281510463"
url = "http://thetechguru.in/?es=subscribe"

I would highly appreciate if someone could help me with the code for this. I rather not use any library for this because I don't want overhead for such small thing. (for only one network call in my app)

This is the piece of code which I am trying but it is not working.

String urlString = "http://www.thetechguru.in/?es=subscribe&es_email=fsdsf@dgd.com&es_name=&es_group=&timestamp=&action=0.9901232281510463";

        String resultToDisplay = "";

        InputStream in = null;
        try {

            URL url = new URL(urlString);

            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

            in = new BufferedInputStream(urlConnection.getInputStream());


        } catch (Exception e) {

            System.out.println(e.getMessage());

            return e.getMessage();

        }

        try {
            resultToDisplay = in.toString();//IOUtils.toString(in, "UTF-8");
            //to [convert][1] byte stream to a string
            Log.v("Response",resultToDisplay);
        } catch (Exception e) {
            e.printStackTrace();
        }

Code executes, but nothing happens, email id is not added in list

What does you Application Manifest file look like? If your application doesn't have permission to access the internet, it won't. Try adding <uses-permission android:name="android.permission.INTERNET" /> to your AndroidMainfest.xml file (inside the <manifest> tag, but before the <application> tag.

Does anything show up in your log (log.v will only show if set to verbose)? If so, please share what it shows. Log guide

Failing that, is the es_email=fsdsf@dgd.com email already in the list? Make sure you're updating the es_email parameter of your URLstring on the button's

I have finally solved the issue. Even though I was using proper code sending POST request, I was unable to subscribe the email id. I checked the HTTP response for the request and found that it was returning error "unexpected-error". I checked in server code and there was this condition of checking HTTP_REFERER in php code. So did a little research and added REFERER in my java request and voila, success!

URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
 //Set to POST
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setDoInput(true);
//Added referer
connection.addRequestProperty("REFERER", "http://thetechguru.in");
connection.setRequestProperty("Content-type", "application/x-www-form-urlencoded");
connection.setReadTimeout(10000);
Writer writer = new OutputStreamWriter(connection.getOutputStream());
writer.write(query);
writer.flush();
writer.close();

I hope it helps someone

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM