Unity WWW类调用http
1、Http请求中Content-Type讲解
MediaType,即是Internet Media Type,互联网媒体类型;也叫做MIME类型,在Http协议消息头中,使用Content-Type来表示具体请求中的媒体类型信息。
类型格式:type/subtype(;parameter)? type
 主类型,任意的字符串,如text,如果是*号代表所有; 
 subtype 子类型,任意的字符串,如html,如果是*号代表所有; 
 parameter 可选,一些参数,如Accept请求头的q参数, Content-Type的 charset参数。 
 例如: Content-Type: text/html;charset:utf-8;
 常见的媒体格式类型如下:
text/html : HTML格式
text/plain :纯文本格式
text/xml : XML格式
image/gif :gif图片格式
image/jpeg :jpg图片格式
image/png:png图片格式 application/xhtml+xml :XHTML格式
application/xml : XML数据格式
application/atom+xml :Atom XML聚合格式
application/json : JSON数据格式
application/pdf :pdf格式
application/msword : Word文档格式
application/octet-stream : 二进制流数据(如常见的文件下载)
application/x-www-form-urlencoded : <form encType=””>中默认的encType,form表单数据被编码为key/value格式发送到服务器(表单默认的提交数据的格式) multipart/form-data : 需要在表单中进行文件上传时,就需要使用该格式
2、示例代码
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine; /// <summary>
/// Get和Post最直观的区别就是GET把参数包含在URL中,POST通过request body传递参数。
/// </summary>
public class HttpTest : MonoBehaviour {
/*
The WWW class can be used to send both GET and POST requests to the server. The WWW class will use GET by default and POST if you supply a postData parameter.
See Also: WWWForm for a way to build valid form data for the postData parameter.
Note: URLs passed to WWW class must be '%' escaped.
Notes http://, https:// and file:// protocols are supported on iPhone. ftp:// protocol support is limited to anonymous downloads only. Other protocols are not supported.
Note: When using file protocol on Windows and Windows Store Apps for accessing local files, you have to specify file:/// (with three slashes).
*/
/// <summary>
/// get方法
/// </summary>
/// <returns></returns>
IEnumerator Get()
{
string fullUrl = @"http://xxxxxx?id=0";//id为参数 using (WWW www = new WWW(fullUrl))
{
yield return www;
if (www.error != null)
{
Debug.LogError("error:" + www.error);
}
Debug.Log(www.text);
}
} /// <summary>
/// application/json连接类型
/// </summary>
/// <returns></returns>
IEnumerator PostJson()
{
string fullUrl = @"http://xxxxxxxx"; //加入http 头信息
Dictionary<string, string> JsonDic = new Dictionary<string, string>();
JsonDic.Add("Content-Type", "application/json"); //body
SendData sendData = new SendData();
sendData.app_key = "1bf5376b82f88384ebe2297327ae2f9fe547dfed";
sendData.time_stamp= "";
sendData.nonce_str= "";
sendData.sign= "1bf5376b82f88384ebe2297327ae2f9fe547dfed";
sendData.img_type= "URL"; byte[] data = System.Text.Encoding.Default.GetBytes(JsonUtility.ToJson(sendData)); using (WWW www = new WWW(fullUrl, data, JsonDic))
{
yield return www;
if (www.error != null)
{
Debug.LogError("error:" + www.error);
}
Debug.Log(www.text);
}
} /// <summary>
/// application/x-www-form-urlencoded连接类型
/// </summary>
/// <returns></returns>
IEnumerator PostForm()
{
string fullUrl = @"http://xxxxxxxx"; //加入http 头信息
Dictionary<string, string> JsonDic = new Dictionary<string, string>();
JsonDic.Add("Content-Type", "application/x-www-form-urlencoded");
JsonDic.Add("cache-control", "no-cache");
JsonDic.Add("Postman-Token", "901451c5-e8c6-4322-8e63-754170323404"); //body
WWWForm form = new WWWForm();
form.AddField("app_key", "1bf5376b82f88384ebe2297327ae2f9fe547dfed");
form.AddField("time_stamp", "");
form.AddField("nonce_str", "");
form.AddField("sign", "1bf5376b82f88384ebe2297327ae2f9fe547dfed");
form.AddField("img_type", "URL"); using (WWW www = new WWW(fullUrl, form.data, JsonDic))
{
yield return www;
if (www.error != null)
{
Debug.LogError("error:" + www.error);
}
Debug.Log(www.text);
}
}
}
[Serializable]
public class SendData
{
public string app_key;
public string time_stamp;
public string nonce_str;
public string sign;
public string img_type;
}
Unity WWW类调用http的更多相关文章
- 转 关于C#中派生类调用基类构造函数的理解
		关于C#中派生类调用基类构造函数的理解 .c#class 本文中的默认构造函数是指在没有编写构造函数的情况下系统默认的无参构造函数 1. 当基类中没有自己编写构造函数时,派生类默认的调用 ... 
- Unity 改变类模板-为你的类添加一个命名空间
		之前在写代码的时候,就很疑惑为什么创建类的时候.没有命名空间呢? 后来自己的类终于和别人写的类名字有冲突.... 如何修改Unity创建类的模板呢? 找到下面这个文件 然后修改 保存文件在Uni ... 
- Spring Boot普通类调用bean
		1 在Spring Boot可以扫描的包下 假设我们编写的工具类为SpringUtil. 如果我们编写的SpringUtil在Spring Boot可以扫描的包下或者使用@ComponentScan引 ... 
- unity android相互调用
		简介 有一些手机功能,Unity没有提供相应的接口,例如震动,例如不锁屏,例如GPS,例如... 有太多的特殊功能Unity都没有提供接口,这时候,我们就需要通过使用Android原生的ADT编辑器去 ... 
- python类方法以及类调用实例方法的理解
		classmethod类方法 1) 在python中.类方法 @classmethod 是一个函数修饰符,它表示接下来的是一个类方法,而对于平常我们见到的则叫做实例方法. 类方法的第一个参数cls,而 ... 
- C#代码使用Process类调用SWFTools工具
		一.Process类调用SWFTools工具将PDF文档转为swf文档 1 string cmdStr = "D:\\SWFTools\\pdf2swf.exe"; string ... 
- c++基类指针指向继承类调用继承类函数
		类里面重载运算符>>, 需要使用友元函数,而友元函数,不能作为虚函数. 所以,基类指针无法直接调用继承类里重构的 >> ; 使用类转换,能解决掉,基类指针 调用 继承类 ... 
- 利用.Net中Process类调用netstat命令来判断计算端口的使用情况
		利用.Net中Process类调用netstat命令来判断计算端口的使用情况: Process p = new Process();p.StartInfo = new ProcessStartInfo ... 
- 17、Spring Boot普通类调用bean【从零开始学Spring Boot】
		转载:http://blog.csdn.net/linxingliang/article/details/52013017 我们知道如果我们要在一个类使用spring提供的bean对象,我们需要把这个 ... 
随机推荐
- Java生成随机数的三种方式
			package cn.zytao.taosir.random; import java.util.Random; public class RandomDemo { private static In ... 
- 如何在GitHub部署自己的个人网站
			前段时间在B站学习了怎样做一个静态网页(up主是 表严肃 ),朋友问我他能不能访问我的这个静态网页,我说还没把它挂到网上.今天看见一篇文章说可以在GitHub部署自己的个人网站,心血来潮,想做一个玩 ... 
- Oracle学习总结(6)—— SQL注入技术
			不管用什么语言编写的Web应用,它们都用一个共同点,具有交互性并且多数是数据库驱动.在网络中,数据库驱动的Web应用随处可见,由此而存在的SQL注入是影响企业运营且最具破坏性的漏洞之一. SQL注入基 ... 
- Redis,Memcache的区别和具体应用场景
			1. Memcached简介 Memcached是以LiveJurnal旗下Danga Interactive公司的Bard Fitzpatric为首开发的高性能分布式内存缓存服务器.其本质上就是一个 ... 
- 使用Swift和SpriteKit写一个忍者游戏
			这篇文章的游戏使用SpriteKit和Swift语言来完毕. SpriteKit是苹果自己的游戏引擎,更能贴合iOS系统底层的API,只是架构和实现上都是模仿了Cocos2D.所以使用上事实上区别不大 ... 
- wpf Shake
			<Setter Property="RenderTransformOrigin" Value="0.5 0.5" /> <Setter Pro ... 
- HDU1237 简单计算器 【栈】+【逆波兰式】
			简单计算器 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ... 
- Swift3.0 闭包整理
			语法表达式 一般形式:{ (parameters) -> returnType in statements } 这里的参数 ... 
- 转:utf8汉字编码16进制对照
			http://blog.chinaunix.net/uid-25544300-id-3281847.html GB Unicode UTF-8 Chinese Character Co ... 
- USACO 2.1 Hamming Codes
			Hamming CodesRob Kolstad Given N, B, and D: Find a set of N codewords (1 <= N <= 64), each of ... 
