gpt4 book ai didi

java - 如何在 Android 中提取 HTTPPost 后的 URL

转载 作者:行者123 更新时间:2023-11-30 04:18:18 25 4
gpt4 key购买 nike

我正在为这个我似乎无法弄清楚的问题而挠头。我要做的是提取 HTTPpost 之后的 URL。这将允许我完成 Oauth 过程。

目前我正在将整个网站提取到我的实体中。

例如:数据将发布到https://mysite.com/login并将在帖子后重定向到 https://mysite.com/dashboard?code=3593085390859082093720

如何提取url?

如果您需要更多信息或可以指导我正确的方向,我们将不胜感激!谢谢!

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.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.content.Intent;
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;
import android.widget.Toast;

public class LoginActivity extends Activity implements OnClickListener {

Button ok,back,exit;
TextView result;

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

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

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

}

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

HttpPost httppost = new HttpPost("https://mysite.com/login");

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

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

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("pseudonym_session[unique_id]", username));
nameValuePairs.add(new BasicNameValuePair("pseudonym_session[password]", password));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

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

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

ResponseHandler<String> responseHandler = new BasicResponseHandler();
String ResponseBody = httpclient.execute(httppost, responseHandler);

Intent intent = new Intent(getBaseContext(), DashboardActivity.class);
startActivity(intent);

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

} 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();
}
}

}

最佳答案

如果您设置了重定向处理程序,您可以从响应中返回服务器将您发送到的位置。这是我刚刚玩过的一段代码……(我应该指出,如果你不设置重定向处理程序,你只会被重定向到最终目的地,这可能是登录屏幕本身)

DefaultHttpClient htc = getHttpClient();
htc.setRedirectHandler(new RedirectHandler() {
@Override
public boolean isRedirectRequested(HttpResponse response, HttpContext context)
{
Log.d(TAG, "isRedirectRequested, response: " + response.toString());
return false;
}

@Override
public URI getLocationURI(HttpResponse response, HttpContext context)
throws ProtocolException
{
Log.d(TAG, "getLocationURI, response: " + response.toString());
return null;
}
});
HttpResponse resp = null;
StringBuilder out = new StringBuilder();
try
{
HttpGet get = new HttpGet(spec);
resp = htc.execute(get);
for (Header hdr : resp.getAllHeaders())
Log.d(TAG, "header " + hdr.getName() + " -> " + hdr.getValue());
...
}
catch (Exception e)
{
Log.e(TAG, "Error connecting to " + spec, e);
return null;
}

关于java - 如何在 Android 中提取 HTTPPost 后的 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9659734/

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