unity使用system.net的post的方法
新学unity 在使用的时候,发现自带的www这个一点也不好用。
比如访问下面的页面。private string url = "http://192.168.10.22/yunwei/e/ywuser/index.php";
private string url = "http://soji.360log.com/e/ywuser/index.php";
两个网址的index.php完全一样,第一个是内部网址,可以返回数据,第二个就不行,一直报错。内网是linux平台,外网是windows平台发面的iis6.0这是唯一的区别 。
改用unitywebrequest这个类也好不到哪去,也报错。
后来没招,自己写了一个类(新手,别笑话我,不太会写类,不知道对不对。)类的代码如下。
但类里面调用了system.net这个命名空间,电脑上运行这个类什么数据都能得到,但发布到安卓 的时候,用户名和密码可以实现,但下面的post的数据就没有返回内容了。
发布的时候,已经用了,net2.0 没有子的,把相应的dll拷过去也没解决问题。不知道哪位大神能指点一下,怎么操作才能让安卓也正常使用。。
调用这个类的代码也放到后面了。
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography;
using System;
using System.Net;
using System.Text;
using System.IO;
namespace PostDatas
{
/// <summary>
/// 扩展类
/// </summary>
public static class Extension
{
/// <summary>
/// 判断字符是否为空
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static bool isEmpty(this string str)
{
if (str.Equals(""))
{
return true;
}
else
{
return false;
}
}
}
//使用的时候直接 这么用,记得在头文件中引用。
// if (userId.isEmpty()
// || password.isEmpty())
// {
// print("账户和密码不能为空");
// return;
// }
public static class PostData
{
/// <summary>
/// 对字符进行MD5加密
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string GetPost(Dictionary<string, string> post)
{
string str = "";
foreach (var item in post)
{
str += item.Key + "=" + item.Value + "&";
}
str = str.Remove(str.Length - 1);
return str;
}
public static string PostWebRequest(string postUrl, string paramData)
{
// 这个是打印,可以注释 了。
// print(postUrl + paramData);
byte[] bytes = Encoding.GetEncoding("gb2312").GetBytes(paramData);
HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(postUrl));
webReq.Method = "POST";
webReq.ContentType = "application/x-www-form-urlencoded;charset=gb2312";
webReq.ContentLength = bytes.Length;
using (Stream newStream = webReq.GetRequestStream())
{
newStream.Write(bytes, 0, bytes.Length);
}
string status = null;//错误状态
HttpWebResponse res;
string str;//返回的str
try
{
res = (HttpWebResponse)webReq.GetResponse();
if (res.StatusCode == HttpStatusCode.NotFound)
{
status = "404";
}
}
catch (WebException ex)
{
res = (HttpWebResponse)ex.Response;
WebExceptionStatus webStatus = ex.Status;
if (webStatus == WebExceptionStatus.ProtocolError)
{
// res = (HttpWebResponse)ex.Response;
status = "404";
}
}
Stream responseStream = res.GetResponseStream();
StreamReader streamReader = new StreamReader(responseStream, Encoding.GetEncoding("UTF-8"));
if (status == null)
{
str = streamReader.ReadToEnd();
}
else
{
str = status;
}
res.Close();
streamReader.Close();
responseStream.Close();
return str;
}
}
}
下面是调用类的代码。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using PostDatas;
public class LoginUser : MonoBehaviour
{
public InputField userNameField;
public InputField passWordField;
public string loginInfo;
private string userName = "";
private string passWord = "";
// private string url = "http://192.168.10.22/yunwei/e/ywuser/index.php";
private string url = "http://soji.360log.com/e/yw11user/index.php";
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void onLogin()
{
userName = userNameField.text;
passWord = passWordField.text;
Text loginInfoText = GameObject.Find("logininfo").GetComponent<Text>();
if (userName.isEmpty())
{
loginInfoText.text = "用户名不能为空";
return;
}
if (passWord.isEmpty())
{
loginInfoText.text = "密码不能为空";
return;
}
//判断网络是否可用。
if (Application.internetReachability == NetworkReachability.NotReachable)
{
//Do sth.
return;
}
else
{
//这地方处理登录
Dictionary<string, string> post = new Dictionary<string, string>();
post.Add("enews", "login");
post.Add("username", userName);
post.Add("password", passWord);
string data = PostData.GetPost(post);
loginInfo = PostData.PostWebRequest(url, data);
if (loginInfo != "")
{
switch (loginInfo)
{
case "404":
loginInfoText.text = "登录网址没找到。";
break;
default:
break;
}
}
else
{
GameObject.Find("logininfo").GetComponent<Text>().text = "登录失败服务器无响应";
}
}
Debug.Log("-------------------------" + loginInfo);
groupID = "1";
applyID = 1;
// SceneManager.LoadScene("02-user");
}
}