如何提取下列URL中的Andr​​oid的HTTPPost下列、URL、Andr、oid

2023-09-06 16:59:26 作者:眼前起雾

我这个问题,我似乎无法弄清楚抓我的头。我正在试图做的是提取HTTPpost以下的URL。这将让我完成OAuth流程。

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

例如:数据将被张贴到 https://mysite.com/login 后会重定向后到 https://mysite.com/dashboard?$c$c=3593085390859082093720

怎样才能提取网址是什么?

如果您需要任何详细信息,或可以直接在我的方向是正确的,所有的AP是preciated!谢谢!

进口java.io.BufferedReader中;进口java.io.IOException异常;进口的java.io.InputStream;进口java.io.InputStreamReader中;进口的java.util.ArrayList;进口的java.util.List;进口org.apache.http.Htt presponse;进口org.apache.http.NameValuePair;进口org.apache.http.client.ClientProtocolException;进口org.apache.http.client.HttpClient;进口org.apache.http.client.ResponseHandler;进口org.apache.http.client.entity.UrlEn codedFormEntity;进口org.apache.http.client.methods.HttpPost;进口org.apache.http.impl.client.BasicResponseHandler;进口org.apache.http.impl.client.DefaultHttpClient;进口org.apache.http.message.BasicNameValuePair;进口android.app.Activity;进口android.content.Intent;进口android.os.Bundle;进口android.util.Log;进口android.view.View;进口android.view.View.OnClickListener;进口android.widget.Button;进口android.widget.EditText;进口android.widget.TextView;进口android.widget.Toast;公共类LoginActivity扩展活动实现OnClickListener {OK按钮,背部,退出;TextView的结果;/ **当第一次创建活动调用。 * /@覆盖公共无效的onCreate(捆绑savedInstanceState){    super.onCreate(savedInstanceState);    的setContentView(R.layout.login);    //登录按钮点击    OK =(按钮)findViewById(R.id.submit);    ok.setOnClickListener(本);    结果=(的TextView)findViewById(R.id.result);}公共无效postLoginData(){    //创建一个新的HttpClient和邮政头    HttpClient的HttpClient的=新DefaultHttpClient();    HttpPost httppost =新HttpPost(https://mysite.com/login);    尝试{        //添加用户名和密码     EditText上的uname =(EditText上)findViewById(R.id.username);     字符串username = uname.getText()的toString()。     的EditText PWORD =(EditText上)findViewById(R.id.password);     。字符串密码= pword.getText()的toString();        清单<&的NameValuePair GT; namevaluepairs中=新的ArrayList<&的NameValuePair GT;(2);        nameValuePairs.add(新BasicNameValuePair(pseudonym_session [UNIQUE_ID],用户名));        nameValuePairs.add(新BasicNameValuePair(pseudonym_session [密码],密码));        httppost.setEntity(新UrlEn codedFormEntity(namevaluepairs中));        //执行HTTP POST请求        Log.w(帆布,执行HTTP POST请求);        HTT presponse响应= httpclient.execute(httppost);        字符串str = inputStreamToString(response.getEntity()的getContent()。)的toString()。        Log.w(画布上,STR);        ResponseHandler所<串GT; ResponseHandler所=新BasicResponseHandler();        字符串ResponseBody = httpclient.execute(httppost,ResponseHandler所);        意向意图=新意图(getBaseContext(),DashboardActivity.class);        startActivity(意向);        如果(str.toString()。equalsIgnoreCase(真))        {         Log.w(帆布,TRUE);         result.setText(登录成功);        }其他        {         Log.w(帆布,假);         result.setText(ResponseBody);        }    }赶上(ClientProtocolException E){     e.printStackTrace();    }赶上(IOException异常五){     e.printStackTrace();    }}私人的StringBuilder inputStreamToString(InputStream为){ 串线=; StringBuilder的总=新的StringBuilder(); //环绕式InputStream的一个BufferedReader RD的BufferedReader =新的BufferedReader(新的InputStreamReader(是)); //读取响应,直到结束 尝试{  而((行= rd.readLine())!= NULL){    total.append(线);  } }赶上(IOException异常五){  e.printStackTrace(); } //返回满弦 总回报;}@覆盖公共无效的onClick(查看视图){  如果(查看== OK){    postLoginData();  }}}

解决方案

如果您设置一个重定向处理,你可以从响应返回服务器发送你的位置。这里的code我只是用玩的一个片段...(我应该指出,如果不设置重定向处理,你只重定向到最终目的地,这可能是登录界面本身)

  DefaultHttpClient HTC = getHttpClient();htc.setRedirectHandler(新RedirectHandler(){  @覆盖  公共布尔isRedirectRequested(HTT presponse响应,HttpContext的背景下)  {    Log.d(TAG,isRedirectRequested,回应:+ response.toString());    返回false;  }  @覆盖  公共URI getLocationURI(HTT presponse响应,HttpContext的背景下)      抛出的ProtocolException  {    Log.d(TAG,getLocationURI,回应:+ response.toString());    返回null;  }});HTT presponse RESP = NULL;StringBuilder的出=新的StringBuilder();尝试{  HTTPGET GET =新HTTPGET(规范);  RESP = htc.execute(获取);  对(HDR标题:resp.getAllHeaders())    Log.d(TAG,头+ hdr.getName()+ - >中+ hdr.getValue());  ...}赶上(例外五){  Log.e(TAG,错误连接+规格,E);  返回null;} 

I'm scratching my head at this problem that I can't seem to figure out. What I'm trying to do is extract the URL following the HTTPpost. This will allow me to go through the Oauth process.

Currently I'm extracting the whole website into my entity.

For example: The data will be posted to https://mysite.com/login and will redirect after the post to https://mysite.com/dashboard?code=3593085390859082093720

How does one extract the url?

If you need any more information or can direct me in the right direction, all is appreciated! Thank you!

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

}

解决方案

if you set a redirect handler, you can get back from the response the location the server's sending you to. here's a snippet of code I was just playing with... (and I should point out, if you DON'T set a redirect handler, you'll just get redirected to the final destination, which might be the login screen itself)

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