@@ -214,6 +214,10 @@ public Request()
214214 /// </summary>
215215 public bool Delete { get ; set ; }
216216 /// <summary>
217+ /// True to send a post method.
218+ /// </summary>
219+ public bool Post { get ; set ; }
220+ /// <summary>
217221 /// The name of the function to invoke on the server.
218222 /// </summary>
219223 public string Function { get ; set ; }
@@ -406,7 +410,7 @@ private IEnumerator ProcessRequestQueue()
406410 Response resp = new Response ( ) ;
407411
408412 DateTime startTime = DateTime . Now ;
409- if ( ! req . Delete )
413+ if ( ! req . Delete && ! req . Post )
410414 {
411415 WWW www = null ;
412416 if ( req . Forms != null )
@@ -549,6 +553,33 @@ private IEnumerator ProcessRequestQueue()
549553
550554 www . Dispose ( ) ;
551555 }
556+ else if ( req . Post )
557+ {
558+ float timeout = Mathf . Max ( Constants . Config . Timeout , req . Timeout ) ;
559+
560+ PostRequest postReq = new PostRequest ( ) ;
561+ Runnable . Run ( postReq . Send ( url , req . Headers ) ) ;
562+ while ( ! postReq . IsComplete )
563+ {
564+ if ( req . Cancel )
565+ break ;
566+ if ( ( DateTime . Now - startTime ) . TotalSeconds > timeout )
567+ break ;
568+ yield return null ;
569+ }
570+
571+ if ( req . Cancel )
572+ continue ;
573+
574+ resp . Success = postReq . Success ;
575+ resp . Data = postReq . Data ;
576+ resp . Error = postReq . Error ;
577+ resp . HttpResponseCode = postReq . HttpResponseCode ;
578+ resp . ElapsedTime = ( float ) ( DateTime . Now - startTime ) . TotalSeconds ;
579+ resp . Headers = postReq . ResponseHeaders ;
580+ if ( req . OnResponse != null )
581+ req . OnResponse ( req , resp ) ;
582+ }
552583 else
553584 {
554585
@@ -695,6 +726,72 @@ public IEnumerator Send(string url, Dictionary<string, string> headers)
695726 IsComplete = true ;
696727 }
697728 } ;
729+
730+ private class PostRequest
731+ {
732+ public string URL { get ; set ; }
733+ public Dictionary < string , string > Headers { get ; set ; }
734+ public bool IsComplete { get ; set ; }
735+ public bool Success { get ; set ; }
736+ public long HttpResponseCode { get ; set ; }
737+ public byte [ ] Data { get ; set ; }
738+ public Error Error { get ; set ; }
739+ public Dictionary < string , string > ResponseHeaders { get ; set ; }
740+
741+ public IEnumerator Send ( string url , Dictionary < string , string > headers )
742+ {
743+ #if ENABLE_DEBUGGING
744+ Log . Debug ( "PostRequest.Send()" , "PostRequest, Send: {0}" , url ) ;
745+ #endif
746+
747+ URL = url ;
748+ Headers = new Dictionary < string , string > ( ) ;
749+ foreach ( var kp in headers )
750+ {
751+ if ( kp . Key != "User-Agent" )
752+ Headers [ kp . Key ] = kp . Value ;
753+ }
754+
755+ #if ! NETFX_CORE
756+ // This fixes the exception thrown by self-signed certificates.
757+ ServicePointManager . ServerCertificateValidationCallback = new RemoteCertificateValidationCallback ( delegate { return true ; } ) ;
758+ #endif
759+
760+ #if ENABLE_DEBUGGING
761+ Log . Debug ( "PostRequest.Send()" , "PostRequest, ProcessRequest {0}" , URL ) ;
762+ #endif
763+ UnityWebRequest postReq = UnityWebRequest . Get ( URL ) ;
764+ postReq . method = UnityWebRequest . kHttpVerbPOST ;
765+ postReq . downloadHandler = new DownloadHandlerBuffer ( ) ;
766+
767+ foreach ( var kp in Headers )
768+ postReq . SetRequestHeader ( kp . Key , kp . Value ) ;
769+ #if UNITY_2017_2_OR_NEWER
770+ yield return postReq . SendWebRequest ( ) ;
771+ #else
772+ yield return postReq . Send ( ) ;
773+ #endif
774+ Error error = null ;
775+ if ( ! string . IsNullOrEmpty ( postReq . error ) )
776+ {
777+ error = new Error ( )
778+ {
779+ URL = url ,
780+ ErrorCode = postReq . responseCode ,
781+ ErrorMessage = postReq . error ,
782+ Response = postReq . downloadHandler . text ,
783+ ResponseHeaders = postReq . GetResponseHeaders ( )
784+ } ;
785+ }
786+
787+ Success = postReq . responseCode == HTTP_STATUS_OK || postReq . responseCode == HTTP_STATUS_OK || postReq . responseCode == HTTP_STATUS_NO_CONTENT || postReq . responseCode == HTTP_STATUS_ACCEPTED ;
788+ HttpResponseCode = postReq . responseCode ;
789+ ResponseHeaders = postReq . GetResponseHeaders ( ) ;
790+ Data = postReq . downloadHandler . data ;
791+ Error = error ;
792+ IsComplete = true ;
793+ }
794+ } ;
698795 #endregion
699796 }
700797}
0 commit comments