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的更多相关文章

  1. 转 关于C#中派生类调用基类构造函数的理解

    关于C#中派生类调用基类构造函数的理解 .c#class       本文中的默认构造函数是指在没有编写构造函数的情况下系统默认的无参构造函数 1.  当基类中没有自己编写构造函数时,派生类默认的调用 ...

  2. Unity 改变类模板-为你的类添加一个命名空间

    之前在写代码的时候,就很疑惑为什么创建类的时候.没有命名空间呢?   后来自己的类终于和别人写的类名字有冲突.... 如何修改Unity创建类的模板呢?  找到下面这个文件 然后修改 保存文件在Uni ...

  3. Spring Boot普通类调用bean

    1 在Spring Boot可以扫描的包下 假设我们编写的工具类为SpringUtil. 如果我们编写的SpringUtil在Spring Boot可以扫描的包下或者使用@ComponentScan引 ...

  4. unity android相互调用

    简介 有一些手机功能,Unity没有提供相应的接口,例如震动,例如不锁屏,例如GPS,例如... 有太多的特殊功能Unity都没有提供接口,这时候,我们就需要通过使用Android原生的ADT编辑器去 ...

  5. python类方法以及类调用实例方法的理解

    classmethod类方法 1) 在python中.类方法 @classmethod 是一个函数修饰符,它表示接下来的是一个类方法,而对于平常我们见到的则叫做实例方法. 类方法的第一个参数cls,而 ...

  6. C#代码使用Process类调用SWFTools工具

    一.Process类调用SWFTools工具将PDF文档转为swf文档 1 string cmdStr = "D:\\SWFTools\\pdf2swf.exe"; string ...

  7. c++基类指针指向继承类调用继承类函数

      类里面重载运算符>>, 需要使用友元函数,而友元函数,不能作为虚函数. 所以,基类指针无法直接调用继承类里重构的 >>  ; 使用类转换,能解决掉,基类指针 调用 继承类 ...

  8. 利用.Net中Process类调用netstat命令来判断计算端口的使用情况

    利用.Net中Process类调用netstat命令来判断计算端口的使用情况: Process p = new Process();p.StartInfo = new ProcessStartInfo ...

  9. 17、Spring Boot普通类调用bean【从零开始学Spring Boot】

    转载:http://blog.csdn.net/linxingliang/article/details/52013017 我们知道如果我们要在一个类使用spring提供的bean对象,我们需要把这个 ...

随机推荐

  1. soapui测试接口使用步骤

    1.新建项目 2. 定义接口 url输入接口 3.新建测试集 选择项目,右键 4.在测试集下新建测试用例 5.在测试步骤中导入要测试的请求 6.run

  2. jquery-ajax基础-XMLHttpRequest

    XMLHttpRequest知识点 原生的ajax代码 var xmlhttp; // 声明一个对象 if (window.XMLHttpRequest) {// code for IE7+, Fir ...

  3. Spring自带字符编码过滤器

    http://blog.csdn.net/youngage/article/details/51356821 http://blog.csdn.net/daelly/article/details/5 ...

  4. [MFC]透明图展示

    (Owed by: 春夜喜雨 http://blog.csdn.net/chunyexiyu 转载请标明来源) 一般我们可见的图形RGB三元色.对Alpha通道的话.它不一定会显示到窗口中来. 在Wi ...

  5. Android 推断程序在手机中是否是活动状态或者正在执行状态

    沈阳斌子在今天项目需求上碰到个这种问题,在Service中须要推断当前的程序是否是活动状态,换句话说也就是说后台跑的服务中有业务需求检測当前程序是否是该服务的程序 这样好让点击推送通知时跳转到不同的页 ...

  6. reactjs simple text editor

    import React, { Component } from 'react' import PubSub from 'pubsub' import GlobalVars from 'globalV ...

  7. MMORPG大型游戏设计与开发(server 游戏场景 事件)

    游戏截图 场景事件 一个完整的对象一般都拥有事件,至于什么是事件在这里就不多解释了. 在场景中的事件在天龙/武侠世界中的事件包含场景初始化.场景定时器.场景退出.玩家进入场景.角色升级.角色死亡.角色 ...

  8. Android自己定义控件系列三:自己定义开关button(二)

    接上一篇自己定义开关button(一)的内容继续.上一次实现了一个开关button的基本功能.即自己定义了一个控件.开关button,实现了点击切换开关状态的功能.今天我们想在此基础之上.进一步实现触 ...

  9. 广东工业大学2016校赛决赛-网络赛 1169 Problem A: Krito的讨伐 优先队列

    Problem A: Krito的讨伐 Description Krito终于干掉了99层的boss,来到了第100层.第100层可以表示成一颗树,这棵树有n个节点(编号从0到n-1),树上每一个节点 ...

  10. lightoj--1410--Consistent Verdicts(技巧)

    Consistent Verdicts Time Limit: 5000MS   Memory Limit: 32768KB   64bit IO Format: %lld & %llu Su ...