概述
可自定义下载路径,默认桌面 ,并自动打开
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Newtonsoft.Json;
using System;
using System.Net;
using System.IO;
using System.Diagnostics;
using UnityEngine.UI;
using UnityEngine.Networking;
using NPOI.XWPF.UserModel;
public class Test_date : MonoBehaviour
{
private Dictionary<string, string> dic = new Dictionary<string, string>();
/// <summary>
/// 网络world地址
/// </summary>
private string url = "http://121.196.15.100/static/attachment/554915940324741120_山大思政实验报告.docx";
/// 网络log地址
/// </summary>
private string url_log = "http://121.196.15.100/api/user/userInfo/login";
/// <summary>
/// 路径
/// </summary>
private string Path = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)+"/admin_fan/";
/// <summary>
/// 账号密码
/// </summary>
WWWForm form;
public Text show;
// private string Path = Application.streamingAssetsPath+"/";
// Start is called before the first frame update
void Start()
{
form = new WWWForm();
form.AddField("loginName", "admin");
form.AddField("password", "123456");
}
public void Update()
{
//登录
if (Input.GetKeyDown(KeyCode.Q))
{
StartCoroutine(SendPost(url_log, form));
}
//下载报告
if (Input.GetKeyDown(KeyCode.W))
{
StartCoroutine(Down(Path, url, "山大思政实验报告.docx"));
}
}
//get
IEnumerator SendGet(string _url)
{
WWW getData = new WWW(_url);
yield return getData;
if (getData.error != null)
{
print(getData.error);
}
else
{
print(getData.text);
}
}
//post
IEnumerator SendPost(string _url, WWWForm _wForm)
{
WWW postData = new WWW(_url, _wForm);
yield return postData;
if (postData.error != null)
{
print(postData.error);
}
else
{
dic= JsonConvert.DeserializeObject<Dictionary<string,string>>(postData.text);
print(postData.text);
foreach (var item in dic)
{
print(item.Key + ":" + item.Value);
}
show.text = postData.text;
}
}
/// <summary>
/// 下载实验报告
/// </summary>
/// <param name="path"></param>
/// <param name="Url"></param>
/// <returns></returns>
IEnumerator Down(string path,string Url,string name)
{
//发送请求
UnityWebRequest webRequest = UnityWebRequest.Get(Url);
yield return webRequest.SendWebRequest();
//检查目录是否存在,不存在则创建
string dir = path.Substring(0, path.LastIndexOf("/"));
if (!Directory.Exists(dir))
Directory.CreateDirectory(dir);
//获取二进制数据
var File = webRequest.downloadHandler.data;
//创建文件写入对象
FileStream nFile = new FileStream(path + "山大思政实验报告.docx", FileMode.Create);
print("dasdas");
//写入数据
nFile.Write(File, 0, File.Length);
nFile.Close();
Process.Start(path + name);//打开指定文件
}
}
最后
以上就是如意电话为你收集整理的unity 从服务器下载world 对接测试登录接口的全部内容,希望文章能够帮你解决unity 从服务器下载world 对接测试登录接口所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复