Unity Post 请求 非 www

在使用Unity的WWW来进行Post请求时候往往会失败。所以得找一个更加牛逼的方式。我们将用HttpWebRequest。



直接代码:




private 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 void BuyTo()
	{
		Dictionary<string, string> post = new Dictionary<string, string>();
		post.Add("name", "xxxx");
		post.Add("appellation", "1");
		post.Add("mobile", m_Phone);
		post.Add("verificationCode", yanzhengma.text);
		post.Add("province", "20");
		post.Add("city", "219");
		post.Add("orderInfo", "{\"39\":1,\"514\":1,\"519\":1,\"524\":1}");

		post.Add("address", "");
		post.Add("isDefault", "0");

		post.Add("payMethod", "book");
		post.Add("getCarAddress", "1");
		post.Add("idCard", "xxxxxxxxxxxxxxxx");


		string data = GetPost(post);
		PostWebRequest (m_Url2 + "create",data);
	}

	private 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);
		}
		using (WebResponse res = webReq.GetResponse())
		{
			
			Stream responseStream = res.GetResponseStream();
			StreamReader streamReader = new StreamReader(responseStream, Encoding.GetEncoding("UTF-8"));
			string str = streamReader.ReadToEnd();
			streamReader.Close();
			responseStream.Close();
			print(str);

			return str;
		}
	}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值