如何捕捉会话密钥生成URL从重定向 [英] How To Capture Session Key Generated From URL Redirector
本文介绍了如何捕捉会话密钥生成URL从重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以我有当查询发送到URL生成一个关键的Web服务。我如何发送一个查询 - 然后捕获Android中返回的关键?
如何在Android的完成这个任务任何工具,教程或平出的建议是AP preciated。
来源:
io.IOException;
进口org.jsoup.Jsoup;
进口org.jsoup.nodes.Document;
进口org.jsoup.nodes.Element;
进口org.jsoup.select.Elements;
进口android.app.Activity;
进口android.app.ProgressDialog;
进口android.os.AsyncTask;
进口android.os.Bundle;
进口android.widget.TextView;公共类MainActivity延伸活动{ TextView的电视;
字符串URL =http://testcloud.edu/apps/users/results.cfm?lname=FOO&fname=BAR;
串TR;
文档文档; @覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main); 电视=(的TextView)findViewById(R.id.TextView01);
新MyTask()执行(URL);
} 私有类MyTask扩展的AsyncTask<弦乐,太虚,字符串> { ProgressDialog PROG; 字符串title =; @覆盖
在preExecute保护无效(){
PROG =新ProgressDialog(MainActivity.this);
prog.setMessage(加载......);
prog.show();
} @覆盖
保护字符串doInBackground(字符串... PARAMS){
尝试{
。DOC = Jsoup.connect(PARAMS [0])获得();
元素tableElement = doc.select(数据网格。)(第)。 元件tableRows = tableElement.select(TR);
对(件行:tableRows){
要素单元= row.select(TD);
如果(cells.size()大于0){
标题= cells.get(0).child(0).attr(的href)+;
+ cells.get(0)的.text()+;
+ cells.get(1)的.text()+;
+ cells.get(2)的.text()+;
+ cells.get(3)的.text();
}
}
}赶上(IOException异常五){
e.printStackTrace();
}
返回称号;
} @覆盖
保护无效onPostExecute(字符串名称){
super.onPostExecute(职称);
prog.dismiss();
tv.setText(职称);
}
}
}
编辑(后第一个答案):
包com.example.test;进口java.io.IOException异常;
进口的java.io.InputStream;
进口java.net.URI中;
进口java.net.URISyntaxException;
进口的java.net.URL;进口org.apache.http.HttpEntity;
进口org.apache.http.Htt presponse;
进口org.apache.http.client.HttpClient;
进口org.apache.http.client.methods.HttpGet;
进口org.apache.http.entity.BufferedHttpEntity;
进口org.apache.http.impl.client.DefaultHttpClient;
进口org.jsoup.Jsoup;
进口org.jsoup.nodes.Document;
进口org.jsoup.nodes.Element;
进口org.jsoup.select.Elements;
进口android.app.Activity;
进口android.app.ProgressDialog;
进口android.graphics.Bitmap;
进口android.graphics.BitmapFactory;
进口android.os.AsyncTask;
进口android.os.Bundle;
进口android.widget.ImageView;
进口android.widget.TextView;公共类MainActivity延伸活动{ TextView的电视;
字符串URL =http://testcloud.edu/apps/users/results.cfm?lname=FOO&fname=BAR;
串TR;
文档文档; @覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main); 电视=(的TextView)findViewById(R.id.TextView01);
新MyTask()执行(URL);
} 私有类MyTask扩展的AsyncTask<弦乐,太虚,字符串> { ProgressDialog PROG; 字符串title =; @覆盖
在preExecute保护无效(){
PROG =新ProgressDialog(MainActivity.this);
prog.setMessage(加载......);
prog.show(); } @覆盖
保护字符串doInBackground(字符串... PARAMS){ ImageView的IMG =(ImageView的)findViewById(R.id.imageView1);
{
尝试{
manualRedirectHandler(URL); 文档manualRedirectHandler(字符串URL)抛出IOException
响应响应= Jsoup.connect(url.replaceAll(,%20))followRedirects(假).execute();
INT状态= response.status code(); 如果(状态== || HttpURLConnection.HTTP_MOVED_TEMP状态== || HttpURLConnection.HTTP_MOVED_PERM状态== HttpURLConnection.HTTP_SEE_OTHER)
{
字符串的redirectUrl = response.header(位置);
的System.out.println(重定向到:+的redirectUrl); //关键是这里
返回manualRedirectHandler(的redirectUrl);
} 返回Jsoup.parse(response.body());
} 字符串IMAGEURL =http://0.tqn.com/d/webclipart/1/0/5/l/4/floral-icon-5.jpg;
HTTPGET HTT prequest = NULL; HTT prequest =新HTTPGET(URI.create(IMAGEURL)); HttpClient的HttpClient的=新DefaultHttpClient();
HTT presponse响应=(HTT presponse)的HttpClient
.execute(HTT prequest); HttpEntity实体= response.getEntity();
BufferedHttpEntity b_entity =新BufferedHttpEntity(实体);
输入的InputStream = b_entity.getContent(); 位图位图= BitmapFactory.de codeStream(输入); img.setImageBitmap(位图); 。DOC = Jsoup.connect(PARAMS [0])获得();
元素tableElement = doc.select(数据网格。)(第)。 元件tableRows = tableElement.select(TR);
对(件行:tableRows){
要素单元= row.select(TD);
如果(cells.size()大于0){
标题= cells.get(0).child(0).attr(的href)+;
+ cells.get(0)的.text()+;
+ cells.get(1)的.text()+;
+ cells.get(2)的.text()+;
+ cells.get(3)的.text();
}
}
}赶上(IOException异常五){
e.printStackTrace();
}
返回称号;
}
} @覆盖
保护无效onPostExecute(字符串名称){
super.onPostExecute(职称);
prog.dismiss();
tv.setText(职称); } }
}
解决方案
你可以找到在重定向的关键....
manualRedirectHandler(URL);私有静态文档manualRedirectHandler(字符串URL)抛出IOException异常
{
响应响应= Jsoup.connect(url.replaceAll(,%20))followRedirects(假).execute();
INT状态= response.status code();如果(状态== || HttpURLConnection.HTTP_MOVED_TEMP状态== || HttpURLConnection.HTTP_MOVED_PERM状态== HttpURLConnection.HTTP_SEE_OTHER)
{
字符串的redirectUrl = response.header(位置);
的System.out.println(重定向到:+的redirectUrl); //关键是这里
返回manualRedirectHandler(的redirectUrl);
}返回Jsoup.parse(response.body());
}
重定向code发现<一个href=\"http://stackoverflow.com/questions/18292919/giving-an-url-that-redirected-is-a-url-with-spaces-to-jsoup-leads-to-an-error\">on这个帖子
http://testcloud.edu/apps/users/redirector.cfm?ID=8b5619c4-f137-45f1-8274-3fbb39b7225d&page=1&&lname=&fname=
http://testcloud.edu/apps/users/details.cfm?id=8b5619c4-f137-45f1-8274-3fbb39b7225d&key=4E2268588778333DC7BBA3C8B219E628&page=1&lname=&fname=
So I have a web service which generates a key when a query is sent to a URL. How can I send a query - then capture the returned key in Android?
Any tools, tutorials or flat out advice on how to accomplish this task in Android is appreciated.
SOURCE:
io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView tv;
String url = "http://testcloud.edu/apps/users/results.cfm?lname=FOO&fname=BAR";
String tr;
Document doc;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.TextView01);
new MyTask().execute(url);
}
private class MyTask extends AsyncTask<String, Void, String> {
ProgressDialog prog;
String title = "";
@Override
protected void onPreExecute() {
prog = new ProgressDialog(MainActivity.this);
prog.setMessage("Loading....");
prog.show();
}
@Override
protected String doInBackground(String... params) {
try {
doc = Jsoup.connect(params[0]).get();
Element tableElement = doc.select(".datagrid").first();
Elements tableRows = tableElement.select("tr");
for (Element row : tableRows) {
Elements cells = row.select("td");
if (cells.size() > 0) {
title = cells.get(0).child(0).attr("href") + " ; "
+cells.get(0).text() + "; "
+ cells.get(1).text() + "; "
+ cells.get(2).text() + "; "
+ cells.get(3).text();
}
}
} catch (IOException e) {
e.printStackTrace();
}
return title;
}
@Override
protected void onPostExecute(String title) {
super.onPostExecute(title);
prog.dismiss();
tv.setText(title);
}
}
}
EDIT (after first answer):
package com.example.test;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView tv;
String url = "http://testcloud.edu/apps/users/results.cfm?lname=FOO&fname=BAR";
String tr;
Document doc;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.TextView01);
new MyTask().execute(url);
}
private class MyTask extends AsyncTask<String, Void, String> {
ProgressDialog prog;
String title = "";
@Override
protected void onPreExecute() {
prog = new ProgressDialog(MainActivity.this);
prog.setMessage("Loading....");
prog.show();
}
@Override
protected String doInBackground(String... params) {
ImageView img = (ImageView) findViewById(R.id.imageView1);
{
try {
manualRedirectHandler(url);
Document manualRedirectHandler(String url) throws IOException{
Response response = Jsoup.connect(url.replaceAll(" ", "%20")).followRedirects(false).execute();
int status = response.statusCode();
if (status == HttpURLConnection.HTTP_MOVED_TEMP || status == HttpURLConnection.HTTP_MOVED_PERM || status == HttpURLConnection.HTTP_SEE_OTHER)
{
String redirectUrl = response.header("location");
System.out.println("Redirect to: " + redirectUrl);//key will be here
return manualRedirectHandler(redirectUrl);
}
return Jsoup.parse(response.body());
}
String imageURL = "http://0.tqn.com/d/webclipart/1/0/5/l/4/floral-icon-5.jpg";
HttpGet httpRequest = null;
httpRequest = new HttpGet(URI.create(imageURL));
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = (HttpResponse) httpclient
.execute(httpRequest);
HttpEntity entity = response.getEntity();
BufferedHttpEntity b_entity = new BufferedHttpEntity(entity);
InputStream input = b_entity.getContent();
Bitmap bitmap = BitmapFactory.decodeStream(input);
img.setImageBitmap(bitmap);
doc = Jsoup.connect(params[0]).get();
Element tableElement = doc.select(".datagrid").first();
Elements tableRows = tableElement.select("tr");
for (Element row : tableRows) {
Elements cells = row.select("td");
if (cells.size() > 0) {
title = cells.get(0).child(0).attr("href") + " ; "
+ cells.get(0).text() + "; "
+ cells.get(1).text() + "; "
+ cells.get(2).text() + "; "
+ cells.get(3).text();
}
}
} catch (IOException e) {
e.printStackTrace();
}
return title;
}
}
@Override
protected void onPostExecute(String title) {
super.onPostExecute(title);
prog.dismiss();
tv.setText(title);
}
}
}
解决方案
you can find the key in the redirect....
manualRedirectHandler(url);
private static Document manualRedirectHandler(String url) throws IOException
{
Response response = Jsoup.connect(url.replaceAll(" ", "%20")).followRedirects(false).execute();
int status = response.statusCode();
if (status == HttpURLConnection.HTTP_MOVED_TEMP || status == HttpURLConnection.HTTP_MOVED_PERM || status == HttpURLConnection.HTTP_SEE_OTHER)
{
String redirectUrl = response.header("location");
System.out.println("Redirect to: " + redirectUrl);//key will be here
return manualRedirectHandler(redirectUrl);
}
return Jsoup.parse(response.body());
}
redirect code found on this post
这篇关于如何捕捉会话密钥生成URL从重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文