简体   繁体   English

将javascript代码转换为android代码-如何在POST请求中添加引荐来源网址?

[英]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. 我的网站上有这个Java脚本代码,当有人订阅我的新闻时该代码就会执行。 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? 是否可以从我的android应用程序代码中调用此post方法来订阅某人的新闻通讯?

I have tried this code here but it is not working. 在这里尝试过此代码但无法正常工作。

When I debug my js code, variable values are coming as 当我调试我的js代码时,变量值以

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 代码执行,但是什么也没有发生,列表中未添加电子邮件ID

What does you Application Manifest file look like? 您的Application Manifest文件是什么样的? 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. 尝试将<uses-permission android:name="android.permission.INTERNET" />到您的AndroidMainfest.xml文件中(在<manifest>标记内部,但在<application>标记之前)。

Does anything show up in your log (log.v will only show if set to verbose)? 日志中是否显示任何内容(仅在设置为verbose时才会显示log.v)? If so, please share what it shows. 如果是这样,请分享显示的内容。 Log guide 日志指南

Failing that, is the es_email=fsdsf@dgd.com email already in the list? 如果没有,es_email = fsdsf @ dgd.com电子邮件已经在列表中了吗? Make sure you're updating the es_email parameter of your URLstring on the button's 确保您正在更新按钮上的URLstring的es_email参数

I have finally solved the issue. 我终于解决了这个问题。 Even though I was using proper code sending POST request, I was unable to subscribe the email id. 即使我使用发送POST请求的正确代码,也无法订阅电子邮件ID。 I checked the HTTP response for the request and found that it was returning error "unexpected-error". 我检查了请求的HTTP响应,发现它返回错误“意外错误”。 I checked in server code and there was this condition of checking HTTP_REFERER in php code. 我检查了服务器代码,并在php代码中检查了HTTP_REFERER。 So did a little research and added REFERER in my java request and voila, success! 因此做了一些研究,并在我的java请求中添加了REFERER,瞧,成功了!

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 我希望它可以帮助某人

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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