gpt4 book ai didi

java - 使用 http post 登录网站

转载 作者:行者123 更新时间:2023-11-30 11:35:23 24 4
gpt4 key购买 nike

我正在尝试创建一个应用程序以登录此网站 http://www.reclameaqui.com.br/当我输入登录名和密码时,应用程序会向我显示:

03-01 22:34:47.220: W/SENCIDE(27717): 403 禁止

禁止

您没有权限访问此服务器上的/areadoconsumidor/。


Apache/2.2.15 (CentOS) 服务器位于www.reclameaqui.com.br 端口80包 com.sencide;

这是代码

    package com.sencide;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class AndroidLogin extends Activity implements OnClickListener {

Button ok,back,exit,site;
TextView result;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

// Login button clicked
site = (Button)findViewById(R.id.button1);
site.setOnClickListener(this);


// Login button clicked
ok = (Button)findViewById(R.id.btn_login);
ok.setOnClickListener(this);

result = (TextView)findViewById(R.id.lbl_result);

}

public void postLoginData() {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();

/* logar no site */
HttpPost httppost = new HttpPost("http://www.reclameaqui.com.br/includes/autenticaUsuario.php");

try {
// Add user name and password
EditText uname = (EditText)findViewById(R.id.txt_username);
String username = uname.getText().toString();

EditText pword = (EditText)findViewById(R.id.txt_password);
String password = pword.getText().toString();

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(6);
nameValuePairs.add(new BasicNameValuePair("identificador", username ));
nameValuePairs.add(new BasicNameValuePair("senha", password ));
nameValuePairs.add(new BasicNameValuePair("urlRedir", "http://www.reclameaqui.com.br/areadoconsumidor/"));
nameValuePairs.add(new BasicNameValuePair("tipo", "cadastro" ));
nameValuePairs.add(new BasicNameValuePair("x", "35" ));
nameValuePairs.add(new BasicNameValuePair("y", "24" ));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

// Execute HTTP Post Request
Log.w("SENCIDE", "Execute HTTP Post Request");
HttpResponse response = httpclient.execute(httppost);

String str = inputStreamToString(response.getEntity().getContent()).toString();
Log.w("SENCIDE", str);

if(str.toString().equalsIgnoreCase("true"))
{
Log.w("SENCIDE", "TRUE");
result.setText("Login successful");
}else
{
Log.w("SENCIDE", "FALSE");
result.setText(str);
}

} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

private StringBuilder inputStreamToString(InputStream is) {
String line = "";
StringBuilder total = new StringBuilder();
// Wrap a BufferedReader around the InputStream
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
// Read response until the end
try {
while ((line = rd.readLine()) != null) {
total.append(line);
}
} catch (IOException e) {
e.printStackTrace();
}
// Return full string
return total;
}

@Override
public void onClick(View view) {
if(view == ok){
postLoginData();
}
}

}

我该怎么做才能使应用程序运行良好?有人帮助我;

最佳答案

您好像发错页面了。

<form action="includes/autenticaUsuario.php" method="post" onsubmit="return valida_form(this)">

表单似乎发布到 http://www.reclameaqui.com.br/includes/autenticaUsuario.php .

它还有隐藏字段:<input type="hidden" name="urlRedir" value="http://www.reclameaqui.com.br/areadoconsumidor/">

<input type="hidden" name="tipo" value="cadastro">

其次,用户名输入命名为<input type="text" name="identificador">密码输入名为<input type="password" name="senha">所以我相信你的 BasicNamveValuePairs 应该更像这样:new BasicNameValuePair("identificador", identificador)new BasicNameValuePair("senha", senha)


它应该看起来像这样:

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
nameValuePairs.add(new BasicNameValuePair("tipo", "cadastro"));
nameValuePairs.add(new BasicNameValuePair("urlRedir", "http://www.reclameaqui.com.br/areadoconsumidor/"));
nameValuePairs.add(new BasicNameValuePair("identificador", identificador));
nameValuePairs.add(new BasicNameValuePair("senha", senha));

关于java - 使用 http post 登录网站,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15169777/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com