您的位置:首页 > 移动开发 > Android开发

工作中技术难点一:android客户端获取服务端验证码图片

2015-10-21 11:44 435 查看
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
String url = Consts.YANZHENG;
result = httpContent(url);
LogUtil.i("验证码", "result=" + result);
//result={"message":"无","content":{"sessionId":"","logPath":"http://183.234.117.210:9090/AppServer/security/createVerifyCodeImage",
//"code":"Dm4j"},"result":"0"}
try {
JSONObject jsonObject=(JSONObject) new JSONObject(result).get("content");
String yzm_logPath = (String) jsonObject.get("logPath");
String yzm_code=(String) jsonObject.get("code");
LogUtil.i("验证码", "yzm_logPath="+yzm_logPath+"yzm_logPath");
Bitmap yzm_imagview=HttpUtils.postImag(yzm_logPath,Sessionid);

handler.obtainMessage(MSG_SUCCESS, yzm_imagview).sendToTarget();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}).start();

// 获取返回的数据和session
private String httpContent(String yANZHENG) {

// TODO Auto-generated method stub
try {
URL url = new URL(yANZHENG);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
/*String cookieval = connection.getHeaderField("Set-cookie");
if (cookieval != null) {
Sessionid = (String) cookieval.subSequence(0,
cookieval.indexOf(";"));
LogUtil.i("注册", "sessionid=" + Sessionid);
}*/
/*if(null!= Sessionid) {
connection.setRequestProperty("Cookie", Sessionid);
}*/
connection.setReadTimeout(20 * 1000);
connection.setConnectTimeout(20 * 1000);
// 设置请求的头
connection.setRequestProperty("Connection", "keep-alive");
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("Content-type",
"application/x-www-form-urlencoded");
connection.setRequestMethod("POST");
/*// 链接服务器
connection.connect();
OutputStream os = connection.getOutputStream();
os.flush();*/
int responseCode = connection.getResponseCode();
if (responseCode == HttpStatus.SC_OK) {
InputStream response_in = connection.getInputStream();   
 
String cookieval = connection.getHeaderField("Set-cookie");
if (cookieval != null) {
Sessionid = (String) cookieval.subSequence(0,
cookieval.indexOf(";"));
LogUtil.i("注册", "Sessionid=" + Sessionid);
}
return chageInputStream(response_in);
}

} catch (Exception e) {
// TODO: handle exception
ExceptionUtil.handleException(e);
}

return "";

}

//请求图片方法

public static Bitmap postImag(String yzm_logPath, String sessionid) {
// TODO Auto-generated method stub
try {
URL url = new URL(yzm_logPath);
connection = (HttpURLConnection) url.openConnection();
if (sessionid != null) {
connection.setRequestProperty("Cookie", ";"+sessionid);
}
connection.setReadTimeout(10 * 1000);
connection.setConnectTimeout(10 * 1000);
connection.setRequestMethod("POST");
// 设置请求的头
connection.setRequestProperty("Connection", "keep-alive");
connection.setRequestProperty("User-Agent",
"Mozilla/5.0 (Windows NT 6.3; WOW64; rv:27.0) "
+ "Gecko/20100101 Firefox/27.0");
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("Content-type",
"application/x-www-form-urlencoded");
// 链接服务器
int Code = connection.getResponseCode();
if (Code == HttpStatus.SC_OK) {
InputStream in = connection.getInputStream();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
int len = 0;
byte[] data = new byte[1024];
try {
while ((len = in.read(data)) != -1) {
stream.write(data, 0, len);
}
return BitmapFactory.decodeByteArray(stream.toByteArray(), 0, stream.toByteArray().length);

} catch (IOException e) {
// TODO Auto-generated catch block
ExceptionUtil.handleException(e);
} finally {
try {
stream.flush();
stream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
ExceptionUtil.handleException(e);
}
}

}
} catch (Exception e) {
// TODO: handle exception
ExceptionUtil.handleException(e);
} finally {
connection.disconnect();

}
return null;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息