@@ -282,16 +282,16 @@ public static string POSTMethod_Extended(ref ExtendedResult extResult, string ur
282282 {
283283 request . Headers . Add ( Header . Name , Header . Value ) ;
284284 }
285-
286- // Set default Content-Type
287- if ( string . IsNullOrEmpty ( request . ContentType ) )
288- {
289- request . ContentType = CONTENTTYPE ;
290- }
291285 }
292286 }
293287 }
294-
288+
289+ // Set default Content-Type
290+ if ( string . IsNullOrEmpty ( request . ContentType ) )
291+ {
292+ request . ContentType = CONTENTTYPE ;
293+ }
294+
295295 if ( request . ContentType . ToLower ( ) == CONTENTTYPE_URLENCODED . ToLower ( ) )
296296 {
297297 byte [ ] byteArray = System . Text . Encoding . UTF8 . GetBytes ( ( ! String . IsNullOrEmpty ( JsonBody ) ) ? JsonBody : "" ) ;
@@ -392,7 +392,6 @@ public static string POSTMethod_Extended(ref ExtendedResult extResult, string ur
392392 return ContentResult ;
393393 }
394394
395-
396395 /// <summary>
397396 /// POST to Resful API sending Json body.
398397 /// </summary>
@@ -848,16 +847,17 @@ public static string GETMethod_Extended(ref ExtendedResult extResult, string url
848847 request . Headers . Add ( Header . Name , Header . Value ) ;
849848 }
850849
851- // Set default Content-Type
852- if ( string . IsNullOrEmpty ( request . ContentType ) )
853- {
854- request . ContentType = CONTENTTYPE ;
855- }
856850 }
857851
858852 }
859853 }
860854
855+ // Set default Content-Type
856+ if ( string . IsNullOrEmpty ( request . ContentType ) )
857+ {
858+ request . ContentType = CONTENTTYPE ;
859+ }
860+
861861 if ( request . ContentType . ToLower ( ) == CONTENTTYPE_URLENCODED . ToLower ( ) )
862862 {
863863 byte [ ] byteArray = System . Text . Encoding . UTF8 . GetBytes ( ( ! String . IsNullOrEmpty ( JsonBody ) ) ? JsonBody : "" ) ;
@@ -999,22 +999,41 @@ public static string WebMethod(string httpMethod, string url, string JsonBody =
999999 request . Headers . Add ( Header . Name , Header . Value ) ;
10001000 }
10011001
1002- // Set default Content-Type
1003- if ( string . IsNullOrEmpty ( request . ContentType ) )
1004- {
1005- request . ContentType = CONTENTTYPE ;
1006- }
10071002 }
10081003
10091004 }
10101005 }
10111006
1012- using ( var streamWriter = new StreamWriter ( request . GetRequestStream ( ) ) )
1007+ // Set default Content-Type
1008+ if ( string . IsNullOrEmpty ( request . ContentType ) )
10131009 {
1014- if ( ! String . IsNullOrEmpty ( JsonBody ) )
1015- streamWriter . Write ( JsonBody ) ;
1010+ request . ContentType = CONTENTTYPE ;
1011+ }
10161012
1017- streamWriter . Flush ( ) ;
1013+ if ( request . ContentType . ToLower ( ) == CONTENTTYPE_URLENCODED . ToLower ( ) )
1014+ {
1015+ byte [ ] byteArray = System . Text . Encoding . UTF8 . GetBytes ( ( ! String . IsNullOrEmpty ( JsonBody ) ) ? JsonBody : "" ) ;
1016+ // Set the ContentLength property of the WebRequest.
1017+ request . ContentLength = byteArray . Length ;
1018+
1019+ using ( var streamWriter = request . GetRequestStream ( ) )
1020+ {
1021+ streamWriter . Write ( byteArray , 0 , byteArray . Length ) ;
1022+ // Close the Stream object.
1023+ streamWriter . Close ( ) ;
1024+ // Get the response.
1025+
1026+ streamWriter . Flush ( ) ;
1027+ }
1028+ }
1029+ else if ( ! String . IsNullOrEmpty ( JsonBody )
1030+ && ! httpMethod . ToUpper ( ) . Contains ( "GET" ) )
1031+ {
1032+ using ( var streamWriter = new StreamWriter ( request . GetRequestStream ( ) ) )
1033+ {
1034+ streamWriter . Write ( JsonBody ) ;
1035+ streamWriter . Flush ( ) ;
1036+ }
10181037 }
10191038
10201039 var httpResponse = ( HttpWebResponse ) request . GetResponse ( ) ;
@@ -1090,17 +1109,16 @@ public static string WebMethod_Extended(ref ExtendedResult extResult, string htt
10901109 {
10911110 request . Headers . Add ( Header . Name , Header . Value ) ;
10921111 }
1093-
1094- // Set default Content-Type
1095- if ( string . IsNullOrEmpty ( request . ContentType ) )
1096- {
1097- request . ContentType = CONTENTTYPE ;
1098- }
10991112 }
1100-
11011113 }
11021114 }
11031115
1116+ // Set default Content-Type
1117+ if ( string . IsNullOrEmpty ( request . ContentType ) )
1118+ {
1119+ request . ContentType = CONTENTTYPE ;
1120+ }
1121+
11041122 if ( request . ContentType . ToLower ( ) == CONTENTTYPE_URLENCODED . ToLower ( ) )
11051123 {
11061124 byte [ ] byteArray = System . Text . Encoding . UTF8 . GetBytes ( ( ! String . IsNullOrEmpty ( JsonBody ) ) ? JsonBody : "" ) ;
0 commit comments