[unity3d]unity跟.net进行http通信

简介:

谈谈今天的学习感受,今天收获最大的就是解决了u3d向.net提交表单,然后.net服务器将接受过来的表单数据保存到sqlserver数据库中。unity3d中wwwform默认的是post提交的。

http 提交数据原理 

http 协议通过 url来获取和提交数据 。提交数据的方式 有两种,一种是get方法,一种是post方法。get一般用于告诉服务器把满足参数的数据发送给回来。

例如:get 的html代码如下:

[html]  view plain copy
  1. <form action="search.php" method ="GET">  
  2.     <username:<inputtypeinputtype="text"name="user"/><br>  
  3.     <password:<inputtypeinputtype="password "name="pwd"/><br>  
  4.      <input type="submit"value="login"/>  
  5. </form >  

post一般是将数据发送给服务器,服务器将这些数据进行处理,比如说存储到数据库。

例如:post的html 代码如下:

[html]  view plain copy
  1. <form action="login.php" method ="POST" >  
  2.     <username:<inputtypeinputtype="text"name="user"/><br>  
  3.     <password:<inputtypeinputtype="password "name="pwd"/><br>  
  4.      <input type="submit"value="login"/>  
  5. </form >  

     其实区别就是提交的方式不一样,点击login按钮后,浏览器地址栏里分别显示如下:

       get方法url为:http://127.0.0.1/serach.php?user=hortor&pwd=123

       post方法url为:http://127.0.0.1

客户端发送表代码:

using UnityEngine; using System.Collections;  public class test : MonoBehaviour {  	private string url = "http://192.168.1.7/plusFile/Handler.ashx"; 	private string urlname; 	void Start () { 		urlname = "丁小未"; 		 	} 	 	void OnGUI() 	{ 		GUILayout.Label("姓名:"); 		urlname = GUILayout.TextField(urlname); 		if(GUILayout.Button("确认提交")) 		{ 			StartCoroutine(myUpdate());	 		} 	} 	 	IEnumerator myUpdate() 	{ 		WWWForm form = new WWWForm(); 		form.AddField("url",urlname); 		WWW w = new WWW(url,form); 		yield return w; 		print(w.data); 		if(w.error!=null) 		{ 			print("错误:"+w.error); 		} 		else 		{ 			print("OK"); 			print(w.text); //服务器端返回的数据 			print("长度:"+w.text.Length.ToString()); 		} 	} } 

效果图:



服务器端接受代码:

<%@ WebHandler Language="C#" Class="Handler" %>  using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data.SqlClient; using System.Data;  public class Handler : IHttpHandler {          public void ProcessRequest (HttpContext context) {         context.Response.ContentType = "text/plain";                  string name = context.Request.Form["url"];         //string name = context.Request.QueryString["url"];         if (name != null)         {             context.Response.Write("我接收到了:"+name);             //context.Response.Write("<font color= 'red'>hello</font>");             Test1 t = new Test1();             t.conn(name);            }         else         {             context.Response.Write("error");         }     }       public bool IsReusable {         get {             return false;         }     }  }   public class Test1 {     SqlConnection dbConnection;     private string sqlInsert;     //private string name;      public Test1()     {              }      public void conn(string name)     {         if (name != null)         {             sqlInsert = "INSERT INTO source(url) VALUES(" + "'"+name+"'" + ")";             openSqlConnection();//打开数据库             doQuery(sqlInsert);         }     }      public void openSqlConnection()     {         dbConnection = new SqlConnection("server=.;database=Student;user id=sa;password=123456");         dbConnection.Open();     }      public void closeSqlConnection()     {         dbConnection.Close();         dbConnection = null;     }      public void doQuery(string strCommand)     {         SqlCommand dbCommand = dbConnection.CreateCommand();         dbCommand.CommandText = strCommand;         int i = dbCommand.ExecuteNonQuery();         dbCommand.Dispose();         dbCommand = null;         if (i > 0)         {             //Response.Write("插入成功");         }     } }

服务器端效果图:




==================== 迂者 丁小未 CSDN博客专栏=================

MyBlog:http://blog.csdn.net/dingxiaowei2013             MyQQ:1213250243

Unity QQ群:858550         cocos2dx QQ群:280818155

====================== 相互学习,共同进步 ===================

 

转载请注明出处:http://blog.csdn.net/dingxiaowei2013/article/details/17099057

欢迎关注我的微博:http://weibo.com/u/2590571922

需要工程文件的请留言!














本文转蓬莱仙羽51CTO博客,原文链接:http://blog.51cto.com/dingxiaowei/1366199,如需转载请自行联系原作者

相关文章
|
2月前
|
Ubuntu Linux Shell
(已解决)Linux环境—bash: wget: command not found; Docker pull报错Error response from daemon: Get https://registry-1.docker.io/v2/: net/http: request canceled
(已成功解决)Linux环境报错—bash: wget: command not found;常见Linux发行版本,Linux中yum、rpm、apt-get、wget的区别;Docker pull报错Error response from daemon: Get https://registry-1.docker.io/v2/: net/http: request canceled
339 68
(已解决)Linux环境—bash: wget: command not found; Docker pull报错Error response from daemon: Get https://registry-1.docker.io/v2/: net/http: request canceled
|
2月前
|
JSON 数据格式
.net HTTP请求类封装
`HttpRequestHelper` 是一个用于简化 HTTP 请求的辅助类,支持发送 GET 和 POST 请求。它使用 `HttpClient` 发起请求,并通过 `Newtonsoft.Json` 处理 JSON 数据。示例展示了如何使用该类发送请求并处理响应。注意事项包括:简单的错误处理、需安装 `Newtonsoft.Json` 依赖,以及建议重用 `HttpClient` 实例以优化性能。
81 2
|
3月前
|
缓存
HTTP 报文解构:深入剖析 HTTP 通信的核心要素
【10月更文挑战第21天】随着网络技术的不断发展和演进,HTTP 报文的形式和功能也可能会发生变化,但对其基本解构的理解始终是掌握 HTTP 通信的关键所在。无论是在传统的 Web 应用中,还是在新兴的网络技术领域,对 HTTP 报文的深入认识都将为我们带来更多的机遇和挑战。
|
6月前
|
数据采集 JSON API
异步方法与HTTP请求:.NET中提高响应速度的实用技巧
本文探讨了在.NET环境下,如何通过异步方法和HTTP请求提高Web爬虫的响应速度和数据抓取效率。介绍了使用HttpClient结合async和await关键字实现异步HTTP请求,避免阻塞主线程,并通过设置代理IP、user-agent和cookie来优化爬虫性能。提供了代码示例,演示了如何集成这些技术以绕过目标网站的反爬机制,实现高效的数据抓取。最后,通过实例展示了如何应用这些技术获取API的JSON数据,强调了这些方法在提升爬虫性能和可靠性方面的重要性。
异步方法与HTTP请求:.NET中提高响应速度的实用技巧
|
4月前
|
API
使用`System.Net.WebClient`类发送HTTP请求来调用阿里云短信API
使用`System.Net.WebClient`类发送HTTP请求来调用阿里云短信API
67 0
|
6月前
|
数据采集 API 开发者
.NET 8新特性:使用ConfigurePrimaryHttpMessageHandler定制HTTP请求
在.NET 8中,通过`ConfigurePrimaryHttpMessageHandler`方法,开发者能更精细地控制HTTP请求,这对于构建高效爬虫尤为重要。此特性支持定制代理IP、管理Cookie与User-Agent,结合多线程技术,有效应对网络限制及提高数据采集效率。示例代码展示了如何设置代理服务器、模拟用户行为及并发请求,从而在遵守网站规则的同时,实现快速稳定的数据抓取。
106 0
.NET 8新特性:使用ConfigurePrimaryHttpMessageHandler定制HTTP请求
|
6月前
|
数据采集 开发框架 .NET
HttpClient在ASP.NET Core中的最佳实践:实现高效的HTTP请求
在现代Web开发中,高效可靠的HTTP请求对应用性能至关重要。ASP.NET Core提供的`HttpClient`是进行这类请求的强大工具。本文探讨其最佳实践,包括全局复用`HttpClient`实例以避免性能问题,通过依赖注入配置预设头部信息;使用代理IP以防IP被限制;设置合理的`User-Agent`和`Cookie`来模拟真实用户行为,提高请求成功率。通过这些策略,可显著增强爬虫或应用的稳定性和效率。
141 0
HttpClient在ASP.NET Core中的最佳实践:实现高效的HTTP请求
|
6月前
|
负载均衡 Java API
深度解析SpringCloud微服务跨域联动:RestTemplate如何驾驭HTTP请求,打造无缝远程通信桥梁
【8月更文挑战第3天】踏入Spring Cloud的微服务世界,服务间的通信至关重要。RestTemplate作为Spring框架的同步客户端工具,以其简便性成为HTTP通信的首选。本文将介绍如何在Spring Cloud环境中运用RestTemplate实现跨服务调用,从配置到实战代码,再到注意事项如错误处理、服务发现与负载均衡策略,帮助你构建高效稳定的微服务系统。
138 2
|
7月前
|
存储 网络安全 数据安全/隐私保护
[flask]使用mTLS双向加密认证http通信
【7月更文挑战第16天】在Flask应用中实现mTLS双向TLS加密认证可增强HTTP通信安全性。步骤包括: 1. 使用OpenSSL为服务器和客户端生成证书和密钥。 2. 配置Flask服务器使用这些证书: - 安装`flask`和`pyopenssl`. - 设置SSL上下文并启用mTLS验证: 注意事项: - 保持证书有效期并及时更新. - 确保证书链信任. - 充分测试mTLS配置.
130 2
|
6月前
|
开发框架 .NET API
.Net Core Console 项目如何使用 HttpClient 与 Web 服务通信
.Net Core Console 项目如何使用 HttpClient 与 Web 服务通信