java - 从 Java Android 填写表单并发送请求

标签 java android http lua

我正在尝试从 Android 的 Java 程序填写表单并向服务器发送请求。首先我尝试在Android上使用Selenium,但我无法启动它,然后我找到了很多关于如何使用HttpPost等的示例,但表单的操作是:

表单名称=“mainform”方法=“POST”操作=“/system/boxuser_edit.lua”

所以我找到了下面的代码,并且尝试了很多可能的组合,但没有成功

    class SendPostReqAsyncTask extends AsyncTask<String, Void, String>{

        @Override
        protected String doInBackground(String... params) {

            String paramUsername = params[0];
            String paramPassword = params[1];

            HttpClient httpClient = new DefaultHttpClient();

    HttpConnectionParams.setConnectionTimeout(httpClient.getParams(),100000);
    HttpConnectionParams.setSoTimeout(httpClient.getParams(), 100000);
    String SID = "xxxxxxxxx";
    HttpPost httpPost = new HttpPost("http://fritz.box/system/boxuser_edit.lua?sid="+SID+"=new");

            BasicNameValuePair emailBasicNameValuePair = new BasicNameValuePair("user", paramUsername);

            BasicNameValuePair passwordBasicNameValuePair = new BasicNameValuePair("password", paramPassword);

            // We add the content that we want to pass with the POST request to as name-value pairs
            //Now we put those sending details to an ArrayList with type safe of NameValuePair
            List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>();
            nameValuePairList.add(usernameBasicNameValuePair);
            nameValuePairList.add(passwordBasicNameValuePair);

            try {
                // UrlEncodedFormEntity is an entity composed of a list of url-encoded pairs. 
                //This is typically useful while sending an HTTP POST request. 
                UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(nameValuePairList);

                // setEntity() hands the entity (here it is urlEncodedFormEntity) to the request.
                httpPost.setEntity(urlEncodedFormEntity);

                try {
                    // HttpResponse is an interface just like HttpPost.
                    //Therefore we can't initialize them
                    HttpResponse httpResponse = httpClient.execute(httpPost);

                    // According to the JAVA API, InputStream constructor do nothing. 
                    //So we can't initialize InputStream although it is not an interface
                    InputStream inputStream = httpResponse.getEntity().getContent();

                    InputStreamReader inputStreamReader = new InputStreamReader(inputStream);

                    BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

                    StringBuilder stringBuilder = new StringBuilder();

                    String bufferedStrChunk = null;

                    while((bufferedStrChunk = bufferedReader.readLine()) != null){
                        stringBuilder.append(bufferedStrChunk);
                    }

                    return stringBuilder.toString();

                } catch (ClientProtocolException cpe) {
                    System.out.println("First Exception caz of HttpResponese :" + cpe);
                    cpe.printStackTrace();
                } catch (IOException ioe) {
                    System.out.println("Second Exception caz of HttpResponse :" + ioe);
                    ioe.printStackTrace();
                }

            } catch (UnsupportedEncodingException uee) {
                System.out.println("An Exception given because of UrlEncodedFormEntity argument :" + uee);
                uee.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
        }           
    }
    SendPostReqAsyncTask sendPostReqAsyncTask = new SendPostReqAsyncTask();
    sendPostReqAsyncTask.execute(givenUsername, givenPassword);     
}

最佳答案

在我们讨论您的代码之前我有很多问题,因为您在这里所做的似乎是正确的。

首先,您是否在 list 文件中请求了访问网络的权限? 如果不是,这就是你的做法:

将此行代码添加到您的 androidManifest.xml

<uses-permission android:name="android.permission.INTERNET" />

但在此之前,您必须检查您传递给 HttpPost 对象的链接是否正常工作,您可以使用网络浏览器来测试它,因为我刚刚这样做了,但似乎它不起作用! !

关于java - 从 Java Android 填写表单并发送请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23080906/

相关文章:

java - AppWidget 布局未显示, "widget failed to load"

android - 我想分享带有单个字幕的多张图片

Node.js HTTP 无法与 Raspberry Pi 上的 Unix Socket 建立连接

asp.net - HTTP 响应行为

angular - Angular 2.0 中的 Http 请求和响应

java - 如何在 Java 中使用正则表达式非捕获组进行字符串替换

java - 如何获取公共(public)存储路径?外部存储(非 SD 卡)

java - 如何使用 Selenium API 禁用来自已加载页面的 Ajax 请求

java - 从 Firestore 获取数据时 Android 加载图标

android - 无法将 Android ADT 安装到 Eclipse Helios (Windows)