一、验证码
这期要从Javaweb传一张图片到Unity,先上Java代码
/**
* 返回验证码图片
*/
private void identity(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//resp.setContentType("image/jpg");
OutputStream os = resp.getOutputStream();
//==把验证码放入Session
//从请求中获取session
HttpSession session = req.getSession();
//生成图片输出到 响应的输出流 并返回生成的字符串
// GifCaptcha gifCaptcha=new GifCaptcha();
MathPngCaptcha captcha = new MathPngCaptcha(180, 80);
String identityKey = captcha.out(os);
//把数据放入session中 key,value
// session.setAttribute("identityKey", identityKey);
session.setAttribute(ProjectParam.SESSION_IDENTITY_KEY, identityKey);
System.out.printf("验证码:%s\n", identityKey);
//记得关闭流
os.flush();
os.close();
}
上面是一个返回验证码 图片的类
然后再到C#代码,方法路径要对应设置TomCat的路径,上期讲过这里就不讲了
private string identityUrl = "http://127.0.0.1:8080/PensionSystem/LoginServlet?method=identity&";
private void Start() {
StartCoroutine(ServletJsp(identityUrl));
}
IEnumerator ServletJsp(string path){
DownloadHandlerTexture downloadTexture = new DownloadHandlerTexture(true);
request.downloadHandler = downloadTexture;
yield return request.SendWebRequest();
if (request.error != null)
{
Debug.Log(request.error);
}
else
{
identityImage.texture = downloadTexture.texture;
Debug.Log(downloadTexture.texture);
}
}
这里图片用的是RawImage,然后identityImage.texture = downloadTexture.texture接收
在开始的时候用start()执行
这样就可以得到验证码图片了
这期要从Javaweb传一张图片到Unity,先上Java代码
/**
* 返回验证码图片
*/
private void identity(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//resp.setContentType("image/jpg");
OutputStream os = resp.getOutputStream();
//==把验证码放入Session
//从请求中获取session
HttpSession session = req.getSession();
//生成图片输出到 响应的输出流 并返回生成的字符串
// GifCaptcha gifCaptcha=new GifCaptcha();
MathPngCaptcha captcha = new MathPngCaptcha(180, 80);
String identityKey = captcha.out(os);
//把数据放入session中 key,value
// session.setAttribute("identityKey", identityKey);
session.setAttribute(ProjectParam.SESSION_IDENTITY_KEY, identityKey);
System.out.printf("验证码:%s\n", identityKey);
//记得关闭流
os.flush();
os.close();
}
上面是一个返回验证码 图片的类
然后再到C#代码,方法路径要对应设置TomCat的路径,上期讲过这里就不讲了
private string identityUrl = "http://127.0.0.1:8080/PensionSystem/LoginServlet?method=identity&";
private void Start() {
StartCoroutine(ServletJsp(identityUrl));
}
IEnumerator ServletJsp(string path){
DownloadHandlerTexture downloadTexture = new DownloadHandlerTexture(true);
request.downloadHandler = downloadTexture;
yield return request.SendWebRequest();
if (request.error != null)
{
Debug.Log(request.error);
}
else
{
identityImage.texture = downloadTexture.texture;
Debug.Log(downloadTexture.texture);
}
}
这里图片用的是RawImage,然后identityImage.texture = downloadTexture.texture接收
在开始的时候用start()执行
这样就可以得到验证码图片了