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对象,我们需要把这个 ...
随机推荐
- vue 动态获取div宽高有时候为0的情况
项目背景: 需要使用echarts进行图表展示.由于div宽高是不固定的,因此需要先获取父级的宽高再把值赋予到图表的div中. 需要使用 this.$nextTick(() => { }) ...
- 人脸识别中的harr特征提取(转)
影响AdaBoost人脸检测训练算法速度很重要的两方面是特征选取和特征计算.选取的特征为矩特征为Haar特征,计算的方法为积分图. (1)Haar特征: Haar特征分为三类:边缘特征.线性特 ...
- 【hihocoder 1032】最长回文子串
[题目链接]:http://hihocoder.com/problemset/problem/1032 [题意] [题解] 原文地址:https://segmentfault.com/a/119000 ...
- 【hihocoder 1304】搜索一·24点
[题目链接]:http://hihocoder.com/problemset/problem/1304 [题意] [题解] 按照题目给的方法搜索就好; 那个方法很棒啊. 注意除0; 然后是浮点数的比较 ...
- 简述Web Service通讯技术的搭建流程
Web Service 基本概念 Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求,轻量级 ...
- jQuery(表单选择器)
- JavaScript原生值与JSON的转换
---------------------------------- ---------------------------- stringify方法的另外两种参数: ---------------- ...
- asp.net mvc--传值-前台->后台
前端传值->后端 一.Model Binding 方式 前台 @model ADMgr.Web.Models.ListModel 后台 [HttpPost] public ActionResul ...
- 关于sql连接查询(内联、左联、右联、全联)
内连接(INNER JOIN)(典型的连接运算,使用像 = 或 <> 之类的比较运算符).包括相等连接和自然连接. 内连接使用比较运算符根据每个表共有的列的值匹配两个表中的 ...
- 【Java基础】--再谈面向对象
面向对象总结 V1.0 2014.9.14 面向对象总结V2.0 2015.8.14 对照之前的J2SE总结,发现现在的似乎更加的注重联系,開始能把细节的东西都编制到知识网络里面,导图的图 ...