工作中技术难点一: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;
}
@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;
}
相关文章推荐
- java-用HttpURLConnection发送Http请求.
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置