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对象,我们需要把这个 ...
随机推荐
- Linux设备驱动--块设备(一)之概念和框架(转)
基本概念 块设备(blockdevice) --- 是一种具有一定结构的随机存取设备,对这种设备的读写是按块进行的,他使用缓冲区来存放暂时的数据,待条件成熟后,从缓存一次性写入设备或者从设备一次性 ...
- 【hiho一下第二周 】Trie树
[题目链接]:http://hihocoder.com/problemset/problem/1014 [题意] [题解] 在字典树的域里面加一个信息cnt; 表示这个节点下面,记录有多少个单词; 在 ...
- docker 下载镜像 ( 以 mysql为例 )
一.官方镜像仓库 https://hub.docker.com/explore/ 二.常用操作 三.使用命令查看 mysql [root@localhost fw]# docker search my ...
- UIButton上字体的对齐方式
设置UIButton上字体的对齐方式,不是用: [Button.titleLabel setTextAlignment:UITextAlignmentCenter]; 而是用: [Button set ...
- 简单的Queue
不考虑好多东西,算法考试中用得到的Queue #include<iostream> using namespace std; const int MAX = 100; struct MyQ ...
- Troubleshooting Failed Requests Using Tracing in IIS 8.5
https://docs.microsoft.com/en-us/iis/troubleshoot/using-failed-request-tracing/troubleshooting-faile ...
- WEEX SDK集成到工程(Integrate to Android) #25
WEEX SDK集成到工程(Integrate to Android) #25 https://github.com/weexteam/article/issues/25
- 15.Linux的文件结构
linux的文件结构和windows不同,没有分区,是树形的结构: /etc:存放配置文件 /lib:编译程序需要的函数库 /usr:包含所有其他内容,比如内核在/usr/src中,/usr/bin存 ...
- UINavi中push控制器的时候隐藏TabBar
当一个UITabbarController管理多个UINavigationController的时候,我们又从这每一个UINavigationController中push一个ViewControll ...
- UIPickerView的自定义视图
UIPickerView允许开发者对列表项进行任意定制 开发者只要实现UIPickerViewDelegate协议中的-pickerView:viewForRow:forComponent: reus ...