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对象,我们需要把这个 ...
随机推荐
- 用Js写贪吃蛇
使用Javascript做贪吃蛇小游戏, 1.自定义地图宽高,蛇的初始速度 2.食物随机出现 3.蛇的样式属性 4.贪吃蛇玩法(吃食物,碰到边界,吃食物后加速,计分,) <!DOCTYPE ht ...
- Statement对象sql注入漏洞的问题
现在通过mysql以及oracle来测试sql注入 漏洞 mysql中的注释# oracle中的注释为-- 所以注入漏洞就产生了 //登录测试 public void login()throw ...
- CF789A. Anastasia and pebbles
/* CF789A. Anastasia and pebbles http://codeforces.com/contest/789/problem/A 水题 题意:有两个背包,每次分别可取k个物品, ...
- oracle导入expdp、导出impdp数据库用户
仅限oracle服务器上执行:把172.16.251.136:1521/orcl的AMI4_2用户导入到192.168.2.30:1521/orclss中的AMI1用户: 关于导入导出更详细的见文章 ...
- Error: CompareBaseObjectsInternal can only be called from the main thread
Posted: 01:39 PM 06-17-2013 hi, we're working on a project where we need to do some calculations on ...
- jenkins启动失败
一:http://localhost:8080已经可以进了 二:修改端口 只是因为8080端口已经有程序占用了,需要修改端口号,但是在jenkins.xml文件中修改端口号,并不起作用. 解决方法是: ...
- java痛苦学习之路[四]---关于struts2-convention-plugin使用
一.struts2-convention-plugin配置文件具体解释 <constant name="struts.convention.actionConfigBuilder&qu ...
- (转)c++ typedef 函数指针详细说明
转自:http://blog.csdn.net/future200x/article/details/5350134 一个函数在编译时被分配一个入口地址,将这个入口地址称为函数的指针,可以用一个指针变 ...
- 0x01 位运算
都比较基础吧. 知识点 1.快速幂和快速乘(这里有一个用long double舍弃精度的做法,但是感觉既不稳又没用) 2.懒人写边目录的时候的k^1 3.lowbit,得到的是低到高第一个1的位.求一 ...
- 2017-3-5 leetcode 442 531 533
今天莫名其妙睡到了中午,很难受... leetcode442 https://leetcode.com/problems/find-all-duplicates-in-an-array/?tab=De ...