Android 通过Volley 模拟登录教务系统 出错,出错原因:系统正忙

汪汪汪汪汪 发布于 2016/05/26 20:01
阅读 373
收藏 0
我想做一个Android端教务系统,通过Volley发送请求。
思路为:1.获取并存储cookie 2.保存网页的__VIEWSTATE值  3.头信息中带上cookie,下载验证码  4.设置头信息post数据  
预期是302重定向到首页,但现在却重定向到了错误页面。
post的值都检查过了,__VIEWSTATE也是通过Jsoup获取到,头信息也设置了,fiddle抓包发现cookie也是设置成功了。难道是Android上的缓存缘故吗?
用python又是成功的
实在想不到是什么原因,还望各位大神指教!

出错信息


以下为模拟登录fiddle抓包

获取到cookie


带上cookie下载验证码


重定向地址

模拟登录时post数据

以下为浏览器登录的重定向地址及post的数据

以下为实现cookie保存、__VIEWSTATE保持、下载验证码、post数据代码

package com.cyj.volley;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.widget.ImageView;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Request.Method;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.cyj.util.LogUtil;
import com.cyj.volleytest.R;

public class VolleyManager {

	private static VolleyManager volleyManager;
	private static RequestQueue requestQueue;
	private static Context context;
	private final String userAgent = "Mozilla/5.0 (Linux; Android 5.1; Google Nexus 4 - 5.1.0 - API 22 - 768x1280 Build/LM";
	
	private VolleyManager(){
		requestQueue = Volley.newRequestQueue(context);
	}
	
	public static VolleyManager getInstance(){
		if(volleyManager == null){
			synchronized (VolleyManager.class) {
				if(volleyManager == null){
					volleyManager = new VolleyManager();
				}
			}
		}
		return volleyManager;
	}
	
	
	/**
	 * 获取cookie并保持
	 * 
	 * @param url
	 * */
	private void _stringRequestGet(String url){
		StringRequest stringRequest = new StringRequest(url, 
				new Response.Listener<String>() {

					@Override
					public void onResponse(String response) {
						Document doc = Jsoup.parse(response);
						String __VIEWSTATE = doc.select("input").first().attr("value");
						saveSettingNote("VIEWSTATE", __VIEWSTATE);
//						LogUtil.i("VolleyManager", "_stringRequestGet, __VIEWSTATE = " + __VIEWSTATE);
					}
				}, 
				new Response.ErrorListener() {

					@Override
					public void onErrorResponse(VolleyError error) {
						LogUtil.e("VolleyManager", error.getMessage(), error);
					}
				}){
			@Override
			protected Response<String> parseNetworkResponse(
					NetworkResponse response) {
				try {
					Map<String, String> map = response.headers;
					String cookie = map.get("Set-Cookie").trim().split(";")[0];
					saveSettingNote("cookie", cookie);
//					LogUtil.i("VolleyManager", "_stringRequestGet, cookie = " + cookie);
					String data = new String(response.data, "UTF-8");
					return Response.success(data, 
							HttpHeaderParser.parseCacheHeaders(response));
				} catch (UnsupportedEncodingException e) {
					return Response.error(new ParseError(e));
				}
			}
		};
		requestQueue.add(stringRequest);
	}
	
	private void __stringRequestGet(String url){
		final Map<String, String> headers = new HashMap<String, String>();
		StringRequest stringRequest = new StringRequest(url, 
				new Response.Listener<String>() {

					@Override
					public void onResponse(String response) {
						LogUtil.d("VolleyManager", "__stringRequestGet, response = " + response);
					}
				}, new Response.ErrorListener() {

					@Override
					public void onErrorResponse(VolleyError arg0) {
						
					}
				}){
			@Override
			public Map<String, String> getHeaders()
					throws AuthFailureError {
				String cookie = getSettingNote("cookie");
				headers.put("Cookie", cookie);
				headers.put("Referer", "http://jw1.wucc.cn/default2.aspx");
				headers.put("User-Agent", userAgent);
				return headers;
			}
		};
	}
	
	
	/**
	 * 带上cookie下载验证码
	 * 
	 * @param url
	 * @param img
	 * */
	private void _downloadImage(String url, final ImageView img){
		ImageRequest imageRequest = new ImageRequest(url, 
				new Response.Listener<Bitmap>() {

					@Override
					public void onResponse(Bitmap bitmap) {
						img.setImageBitmap(bitmap);
					}
				}, 0, 0, Config.RGB_565, 
				new Response.ErrorListener() {

					@Override
					public void onErrorResponse(VolleyError error) {
						img.setImageResource(R.drawable.ic_launcher);
					}
				}){
			@Override
			public Map<String, String> getHeaders()
					throws AuthFailureError {
				Map<String, String> headers = new HashMap<String, String>();
				String cookie = getSettingNote("cookie");
				headers.put("Cookie", cookie);
//				LogUtil.i("VolleyManager", "_downloadImage, cookie = " + cookie);
				headers.put("Referer", "http://jw1.wucc.cn/");
				headers.put("User-Agent", userAgent);
//				LogUtil.i("VolleyManager", "_downloadImage, headers = " + headers);
				return headers;
			}
		};
		requestQueue.add(imageRequest);
	}
	
	/**
	 * 添加头信息POST发送数据
	 * 
	 * @param url
	 * @param username
	 * @param password
	 * @param checkcode
	 * */
	private void _stringRequestPost(String url, final Map<String, String> headers, 
			final Map<String, String> data){
		StringRequest stringRequest = new StringRequest(Method.POST, url, 
				new Response.Listener<String>() {

					@Override
					public void onResponse(String response) {
						LogUtil.e("VolleyManager", "_stringRequestPost, response = " + response);
					}
				}, 
				new Response.ErrorListener() {

					@Override
					public void onErrorResponse(VolleyError error) {
						LogUtil.e("VolleyManager", error.getMessage(), error);
					}
				}){
			@Override
			public Map<String, String> getHeaders()
					throws AuthFailureError {
				String cookie = getSettingNote("cookie");
				headers.put("Cookie", cookie);
				headers.put("User-Agent", userAgent);
//				LogUtil.i("VolleyManager", "_stringRequestPost, headers = " + headers.toString());
				return headers;
			}
			@Override
			protected Map<String, String> getParams()
					throws AuthFailureError {
				data.put("__VIEWSTATE", getSettingNote("VIEWSTATE"));
//				LogUtil.i("VolleyManager", "_stringRequestPost, data = " + data.toString());
				return data;
			}
			@Override
			protected Response<String> parseNetworkResponse(
					NetworkResponse response) {
				String str = null;
				try {
					str = new String(response.data, "gb2312");
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
				return Response.success(str, HttpHeaderParser.parseCacheHeaders(response));
			}
		};
		requestQueue.add(stringRequest);
	}
	
	
	
	/***********************对外方法***********************/
	
	public static void setContext(Context context){
		VolleyManager.context = context;
	}
	
	public static void stringRequestGet(String url){
		getInstance()._stringRequestGet(url);
	}
	
	public static void downloadImage(String url, ImageView img){
		getInstance()._downloadImage(url, img);
	}
	
	public static void stringRequestPost(String url, Map<String, String> headers, 
			Map<String, String> data){
		getInstance()._stringRequestPost(url, headers, data);
	}

	public static void _stringRequestPost(String url){
		getInstance().__stringRequestGet(url);
	}
	
	/************************辅助方法*************************/
	/**
	 * 保存cookie
	 * */
	private void saveSettingNote(String key,String value){
        SharedPreferences.Editor note = context.getSharedPreferences("values",
        		Context.MODE_PRIVATE)
        		.edit();
        note.putString(key, value);
//        LogUtil.d("VolleyManager", "save setting note, data = " + value);
        note.commit();
    }
	
	/**
	 * 读取cookie
	 * */
	private String getSettingNote(String key){
        SharedPreferences read = context.getSharedPreferences("values", Context.MODE_PRIVATE);
//        LogUtil.d("VolleyManager", "get setting note, data = " + read.getString(key, ""));
        return read.getString(key, "");
    }
	
}



先在这里谢过各位!

加载中
0
OSC首席过客
OSC首席过客
很简单的东西,其实你直接SESSION一个值就行
汪汪汪汪汪
回复 @OSC首席过客 : ASP.NET_SessionId=iwkej5apryvljm2gpatmk5ao 没有不全呀 可能是cookie出的问题 那我试试nohttp
OSC首席过客
OSC首席过客
回复 @汪汪汪汪汪 : 建议使用NOHTTP
OSC首席过客
OSC首席过客
回复 @汪汪汪汪汪 : 你的COOKIE不全吧
汪汪汪汪汪
请问能具体点吗,不太能理解? 您说的同一个session是不是可以这样理解:设置头信息为相同的cookie,可我已经设置了呀
0
汪汪汪汪汪
各位还有别的解决方法吗?
0
汪汪汪汪汪
还有人能看到这个问题吗?
返回顶部
顶部