以编程方式在 Android 中提交 Web 表单
我正在尝试登录网站并在 android 中以编程方式加载网页。意思是,我有密码和登录名,需要提交网络表单并获取响应页面。我在这里尝试了代码:Doing HTTP Post with Android 但我想我可能做错了。
如果这是我尝试访问的网站:http://goo.gl/eiBhP 我的代码是
HttpClient httpclient = new DefaultHttpClient(httpParameters);
HttpPost httppost = new HttpPost(Constants.MAIN_URL);
List<namevaluepair> nameValuePairs = new ArrayList<namevaluepair>(2);
nameValuePairs.add(new BasicNameValuePair("username", "correctusername"));
nameValuePairs.add(new BasicNameValuePair("password", "correctpassword"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpClient.execute(httpost);
然后我应该能够使用它
BufferedReader br = new BufferedReader(new InputStreamReader(
entity.getContent()), 8096);
来获取响应。网站上的登录 ID 和密码包括用户名和密码。我还应该以某种方式将按钮作为名称值对提交吗?我似乎无法让它工作,它只是返回登录页面。请帮忙。我尝试阅读其他类似的问题,但似乎无法使其发挥作用。
I am trying to log into a site and load a webpage programatically in android. Meaning, I have the password and login and need to submit a webform and get the response page. I tried the code here: Doing HTTP Post with Android
but I think I may be doing it wrong.
If this is the site I'm trying to access: http://goo.gl/eiBhP
and my code is
HttpClient httpclient = new DefaultHttpClient(httpParameters);
HttpPost httppost = new HttpPost(Constants.MAIN_URL);
List<namevaluepair> nameValuePairs = new ArrayList<namevaluepair>(2);
nameValuePairs.add(new BasicNameValuePair("username", "correctusername"));
nameValuePairs.add(new BasicNameValuePair("password", "correctpassword"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpClient.execute(httpost);
Then I should be able to use
BufferedReader br = new BufferedReader(new InputStreamReader(
entity.getContent()), 8096);
to get the response. The id of the login and pass on the site ate username and password. should I also somehow submit the button as a name value pair? I cant seem to get this to work, it just returns the login page. Please Help. I've tried reading over the other similar questions but I can't seem to get it to work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
基本上,您需要确保您的代码提交的信息与网页完全相同。
正如 Selvin 指出的那样,该网站很有可能正在使用某种形式的跟踪 - 无论是隐藏的输入值、cookie 还是其他一些基于状态的数据。
您需要查看登录网页的源代码,并了解提交登录详细信息时它在做什么 - 您不一定需要知道所有值的含义,但您的代码必须提交相同的 POST 数据。
如果网站使用状态信息,您将无法在代码中对这些输入值进行硬编码。您可能需要每次使用 HTTP GET 请求检索登录网页的新实例,然后解析数据以提取相关状态数据。不要忘记,他们也可能使用您可能需要提交的 cookie。
总而言之,您可能需要做更多的工作才能使其进入工作状态。并不是试图劝阻您(我不知道您想要实现什么目的),但也许使用该网站更容易!
Basically, you need to make sure that your code is submitting exactly the same information as the webpage.
As Selvin points out, there's a good chance that the website is using some form of tracking - be it in hidden input values, cookies or some other state-based data.
You need to look at the source of the login webpage and understand what it is doing when you submit login details - you don't necessarily need to know what all the values mean, but your code must submit the same POST data.
If the website is using state information, you won't be able to hard-code those input values in your code. You'll probably need to retrieve a new instance of the login webpage each time using a HTTP GET request and then parse the data to extract the relevant state data. Don't forget that they may also be using cookies which you may need to submit.
All in all, you probably need to do a lot more work to get it to a working state. Not trying to dissuade you (and I don't know what you're trying to achieve), but perhaps it's easier just to use the website!