1

Getting Error 4 when calling from C# HttpClient

(13/10/2017 06:30:37 отредактировано karthik)

Тема: Getting Error 4 when calling from C# HttpClient

I am hitting the batch command using C# HttpClient class and keep on getting error code 4 but absolutely the below format is fine.

I am also encoding and sending the payload like below

var content = new StringContent(HttpUtility.UrlEncode(mainParam), Encoding.UTF8, "application/x-www-form-urlencoded");

mainParam is shown below, can you tell me how to fix this issue please ?

svc=core/batch&params={"params":[{"svc":"unit/update_device_type","params":{"itemId":16141282,"deviceTypeId":14806641,"uniqueId":"352094082333714","dataFlags":1}},{"svc":"unit/update_unique_id2","params":{"itemId":16141282,"uniqueId2":""}},{"svc":"unit/update_phone","params":{"itemId":16141282,"phoneNumber":""}},{"svc":"unit/update_phone","params":{"itemId":16141282,"phoneNumber":""}},{"svc":"unit/update_access_password","params":{"itemId":16141282,"accessPassword":""}},{"svc":"unit/update_mileage_counter","params":{"itemId":16141282,"newValue":0}},{"svc":"unit/update_eh_counter","params":{"itemId":16141282,"newValue":0}},{"svc":"unit/update_calc_flags","params":{"itemId":16141282,"newValue":1809}},{"svc":"unit/update_traffic_counter","params":{"itemId":16141282,"newValue":0,"regReset":0}},{"svc":"unit/update_image","params":{"itemId":16141282,"libId":0,"path":"/library/unit/default.png"}},{"svc":"item/update_custom_field","params":{"itemId":16141282,"callMode":"create","id":0,"n":"Chassis Number","v":""}},{"svc":"item/update_custom_field","params":{"itemId":16141282,"callMode":"create","id":0,"n":"Fuel Id","v":""}},{"svc":"item/update_custom_field","params":{"itemId":16141282,"callMode":"create","id":0,"n":"Install Date","v":"8/22/2017"}},{"svc":"item/update_custom_field","params":{"itemId":16141282,"callMode":"create","id":0,"n":"Install Technician Name","v":""}},{"svc":"item/update_custom_field","params":{"itemId":16141282,"callMode":"create","id":0,"n":"License Country","v":""}},{"svc":"item/update_custom_field","params":{"itemId":16141282,"callMode":"create","id":0,"n":"License Expiration Date","v":""}},{"svc":"item/update_custom_field","params":{"itemId":16141282,"callMode":"create","id":0,"n":"License State","v":"  "}},{"svc":"item/update_admin_field","params":{"itemId":16141282,"callMode":"create","id":0,"n":"Device Type","v":"LT500"}},{"svc":"item/update_profile_field","params":{"itemId":16141282,"n":"brand","v":"TOYOTA"}},{"svc":"item/update_profile_field","params":{"itemId":16141282,"n":"model","v":"COROLLA"}},{"svc":"item/update_profile_field","params":{"itemId":16141282,"n":"year","v":""}},{"svc":"unit/update_sensor","params":{"id":1,"n":"Driver Binding","t":"driver","d":"","m":"","p":"drvr_code_r","f":0,"c":"{\"act\":1,\"appear_in_popup\":true,\"ci\":{},\"cm\":0,\"mu\":0,\"pos\":17,\"show_time\":false,\"timeout\":0,\"uct\":0,\"unbound_code\":\"\",\"validate_driver_unbound\":0}","vt":0,"vs":0,"tbl":[],"itemId":16141282,"callMode":"create"}},{"svc":"unit/update_sensor","params":{"id":2,"n":"Operating Time","t":"custom","d":"","m":"hrs","p":"none","f":0,"c":"{\"act\":1,\"appear_in_popup\":false,\"ci\":{},\"cm\":0,\"mu\":0,\"pos\":5,\"show_time\":false,\"text_params\":0,\"timeout\":0,\"uct\":0}","vt":0,"vs":0,"tbl":[],"itemId":16141282,"callMode":"create"}},{"svc":"unit/update_sensor","params":{"id":3,"n":"Odometer","t":"mileage","d":"","m":"km","p":"io16_const/1000","f":0,"c":"{\"act\":1,\"appear_in_popup\":false,\"ci\":{},\"cm\":0,\"mu\":0,\"pos\":4,\"show_time\":false,\"timeout\":0,\"uct\":0}","vt":0,"vs":0,"tbl":[],"itemId":16141282,"callMode":"create"}},{"svc":"unit/update_sensor","params":{"id":4,"n":"Power","t":"voltage","d":"","m":"V","p":"pwr_ext","f":0,"c":"{\"act\":1,\"appear_in_popup\":true,\"ci\":{},\"cm\":0,\"mu\":1,\"pos\":1,\"show_time\":false,\"timeout\":0,\"uct\":0}","vt":0,"vs":0,"tbl":[],"itemId":16141282,"callMode":"create"}},{"svc":"unit/update_sensor","params":{"id":5,"n":"Moving Time","t":"custom","d":"","m":"hrs","p":"none","f":0,"c":"{\"act\":1,\"appear_in_popup\":false,\"ci\":{},\"cm\":0,\"mu\":0,\"pos\":6,\"show_time\":false,\"text_params\":0,\"timeout\":0,\"uct\":0}","vt":0,"vs":0,"tbl":[],"itemId":16141282,"callMode":"create"}},{"svc":"unit/update_sensor","params":{"id":6,"n":"Digital IP2","t":"digital","d":"","m":"","p":"io_3","f":0,"c":"{\"act\":1,\"appear_in_popup\":true,\"ci\":{\"0\":{\"c\":0,\"t\":\"Low\"},\"1\":{\"c\":0,\"t\":\"High\"}},\"cm\":1,\"mu\":1,\"pos\":11,\"show_time\":false,\"timeout\":0,\"uct\":0}","vt":0,"vs":0,"tbl":[],"itemId":16141282,"callMode":"create"}},{"svc":"unit/update_sensor","params":{"id":7,"n":"Digital IP1","t":"digital","d":"","m":"","p":"io_2","f":0,"c":"{\"act\":1,\"appear_in_popup\":true,\"ci\":{\"0\":{\"c\":0,\"t\":\"Low\"},\"1\":{\"c\":0,\"t\":\"High\"}},\"cm\":1,\"mu\":1,\"pos\":10,\"show_time\":false,\"timeout\":0,\"uct\":0}","vt":0,"vs":0,"tbl":[],"itemId":16141282,"callMode":"create"}},{"svc":"unit/update_sensor","params":{"id":8,"n":"Program Ver","t":"custom","d":"","m":"","p":"none","f":0,"c":"{\"act\":1,\"appear_in_popup\":false,\"ci\":{\"12800\":{\"c\":0,\"t\":\"50.0\"},\"12801\":{\"c\":0,\"t\":\"50.1\"},\"12803\":{\"c\":0,\"t\":\"50.3\"},\"12805\":{\"c\":0,\"t\":\"50.5\"},\"12806\":{\"c\":0,\"t\":\"50.6\"},\"12810\":{\"c\":0,\"t\":\"50.10\"},\"12812\":{\"c\":0,\"t\":\"50.12\"},\"12813\":{\"c\":0,\"t\":\"50.13\"},\"13056\":{\"c\":0,\"t\":\"51.0\"},\"13057\":{\"c\":0,\"t\":\"51.1\"},\"13059\":{\"c\":0,\"t\":\"51.3\"},\"13063\":{\"c\":0,\"t\":\"51.7\"},\"13064\":{\"c\":0,\"t\":\"51.8\"},\"13065\":{\"c\":0,\"t\":\"51.9\"},\"13067\":{\"c\":0,\"t\":\"51.11\"}},\"cm\":1,\"mu\":0,\"pos\":12,\"show_time\":false,\"text_params\":0,\"timeout\":0,\"uct\":0}","vt":0,"vs":0,"tbl":[],"itemId":16141282,"callMode":"create"}},{"svc":"unit/update_sensor","params":{"id":9,"n":"Speed Max","t":"custom","d":"","m":"km/h","p":"io_24","f":0,"c":"{\"act\":1,\"appear_in_popup\":false,\"ci\":{},\"cm\":0,\"mu\":0,\"pos\":9,\"show_time\":false,\"text_params\":0,\"timeout\":0,\"uct\":0}","vt":0,"vs":0,"tbl":[],"itemId":16141282,"callMode":"create"}},{"svc":"unit/update_sensor","params":{"id":10,"n":"Network ID & Channel","t":"custom","d":"","m":"","p":"io_205","f":0,"c":"{\"act\":1,\"appear_in_popup\":true,\"ci\":{},\"cm\":0,\"mu\":0,\"pos\":16,\"show_time\":false,\"text_params\":0,\"timeout\":0,\"uct\":0}","vt":0,"vs":0,"tbl":[],"itemId":16141282,"callMode":"create"}},{"svc":"unit/update_sensor","params":{"id":11,"n":"Location Area Code","t":"custom","d":"","m":"","p":"io_206","f":0,"c":"{\"act\":1,\"appear_in_popup\":true,\"ci\":{},\"cm\":0,\"mu\":0,\"pos\":15,\"show_time\":false,\"text_params\":0,\"timeout\":0,\"uct\":0}","vt":0,"vs":0,"tbl":[],"itemId":16141282,"callMode":"create"}},{"svc":"unit/update_sensor","params":{"id":12,"n":"Carrier ID","t":"custom","d":"","m":"","p":"io_241","f":0,"c":"{\"act\":1,\"appear_in_popup\":true,\"ci\":{},\"cm\":0,\"mu\":0,\"pos\":14,\"show_time\":false,\"text_params\":0,\"timeout\":0,\"uct\":0}","vt":0,"vs":0,"tbl":[],"itemId":16141282,"callMode":"create"}},{"svc":"unit/update_sensor","params":{"id":13,"n":"Internal Battery","t":"voltage","d":"","m":"V","p":"pwr_int","f":0,"c":"{\"act\":1,\"appear_in_popup\":true,\"ci\":{},\"cm\":0,\"mu\":1,\"pos\":8,\"show_time\":false,\"timeout\":0,\"uct\":0}","vt":0,"vs":0,"tbl":[],"itemId":16141282,"callMode":"create"}},{"svc":"unit/update_sensor","params":{"id":14,"n":"Ignition","t":"engine operation","d":"","m":"","p":"io_239","f":0,"c":"{\"act\":1,\"appear_in_popup\":true,\"ci\":{\"0\":{\"c\":0,\"t\":\"Off\"},\"1\":{\"c\":0,\"t\":\"On\"}},\"cm\":1,\"consumption\":2,\"mu\":1,\"pos\":2,\"show_time\":false,\"timeout\":0,\"uct\":0}","vt":0,"vs":0,"tbl":[],"itemId":16141282,"callMode":"create"}},{"svc":"unit/update_sensor","params":{"id":15,"n":"Device Type","t":"custom","d":"","m":"","p":"none","f":0,"c":"{\"act\":1,\"appear_in_popup\":false,\"ci\":{\"12800\":{\"c\":0,\"t\":\"LT-1100-GP_Script_50.0_NoIP_NoAPN\"},\"12801\":{\"c\":0,\"t\":\"LT-1100-GP_Script_50.1_FL_Kore\"},\"12803\":{\"c\":0,\"t\":\"LT-1100-GP_Script_50.3_DG_Digicel\"},\"12805\":{\"c\":0,\"t\":\"LT-1100-GP_Script_50.5_DG_DigicelTT\"},\"12806\":{\"c\":0,\"t\":\"LT-1100-GP_Script_50.6_FL_AerisGlobal\"},\"12810\":{\"c\":0,\"t\":\"LT-1100-GP_Script_50.10_DG_DigicelAnguilla\"},\"12812\":{\"c\":0,\"t\":\"LT-1100-GP_Script_50.12_DG_Atrams\"},\"12813\":{\"c\":0,\"t\":\"LT-1100-GP_Script_50.13_DG_Giscad\"},\"13056\":{\"c\":0,\"t\":\"LT-1100-UM_Script_51.0_NoIP_NoAPN\"},\"13057\":{\"c\":0,\"t\":\"LT-1100-UM_Script_51.1_FL_Kore\"},\"13059\":{\"c\":0,\"t\":\"LT-1100-UM_Script_51.3_DG_Digicel\"},\"13063\":{\"c\":0,\"t\":\"LT-1100-UM_Script_51.7_FL_Altaworx\"},\"13064\":{\"c\":0,\"t\":\"LT-1100-UM_Script_51.8_DG_DigicelBVI\"},\"13065\":{\"c\":0,\"t\":\"LT-1100-UM_Script_51.9_DG_DigicelCayman\"},\"13067\":{\"c\":0,\"t\":\"LT-1100-UM_Script_51.11_FL_AerisDualMode\"}},\"cm\":1,\"mu\":0,\"pos\":13,\"show_time\":false,\"text_params\":1,\"timeout\":0,\"uct\":0}","vt":0,"vs":0,"tbl":[],"itemId":16141282,"callMode":"create"}},{"svc":"unit/update_sensor","params":{"id":16,"n":"Event","t":"custom","d":"","m":"","p":"event_code","f":0,"c":"{\"act\":1,\"appear_in_popup\":false,\"ci\":{\"1\":{\"c\":0,\"t\":\"Moving\"},\"10\":{\"c\":0,\"t\":\"On Backup Battery\"},\"11\":{\"c\":0,\"t\":\"Low Power Begin\"},\"12\":{\"c\":0,\"t\":\"Low Power End\"},\"13\":{\"c\":0,\"t\":\"Power On\"},\"14\":{\"c\":0,\"t\":\"Input2 On\"},\"15\":{\"c\":0,\"t\":\"Input2 Off\"},\"16\":{\"c\":0,\"t\":\"Idling\"},\"17\":{\"c\":0,\"t\":\"On Demand\"},\"18\":{\"c\":0,\"t\":\"N\\/A\"},\"2\":{\"c\":0,\"t\":\"Parked\"},\"3\":{\"c\":0,\"t\":\"Ignition On\"},\"4\":{\"c\":0,\"t\":\"Ignition Off\"},\"5\":{\"c\":0,\"t\":\"Start\"},\"6\":{\"c\":0,\"t\":\"Stop\"},\"7\":{\"c\":0,\"t\":\"Input1 On\"},\"8\":{\"c\":0,\"t\":\"Input1 Off\"},\"9\":{\"c\":0,\"t\":\"Emergency\"}},\"cm\":1,\"mu\":0,\"pos\":3,\"show_time\":false,\"text_params\":1,\"timeout\":0,\"uct\":0}","vt":0,"vs":0,"tbl":[],"itemId":16141282,"callMode":"create"}},{"svc":"unit/update_sensor","params":{"id":17,"n":"Cell RSSI","t":"custom","d":"","m":"db","p":"rssi","f":0,"c":"{\"act\":1,\"appear_in_popup\":false,\"ci\":{},\"cm\":0,\"mu\":0,\"pos\":18,\"show_time\":false,\"text_params\":0,\"timeout\":0,\"uct\":0}","vt":0,"vs":0,"tbl":[],"itemId":16141282,"callMode":"create"}},{"svc":"unit/update_sensor","params":{"id":18,"n":"GPS Signal","t":"custom","d":"","m":"","p":"sats","f":0,"c":"{\"act\":1,\"appear_in_popup\":true,\"ci\":{\"10\":{\"c\":0,\"t\":\"Excellent\"},\"2\":{\"c\":0,\"t\":\"No Signal\"},\"4\":{\"c\":0,\"t\":\"Very Poor\"},\"6\":{\"c\":0,\"t\":\"Poor\"},\"7\":{\"c\":0,\"t\":\"Average\"},\"8\":{\"c\":0,\"t\":\"Good\"},\"9\":{\"c\":0,\"t\":\"Very Good\"}},\"cm\":1,\"mu\":0,\"pos\":20,\"show_time\":false,\"text_params\":0,\"timeout\":0,\"uct\":0}","vt":0,"vs":0,"tbl":[],"itemId":16141282,"callMode":"create"}},{"svc":"unit/update_sensor","params":{"id":19,"n":"Cell Signal","t":"custom","d":"","m":"","p":"io_21","f":0,"c":"{\"act\":1,\"appear_in_popup\":true,\"ci\":{\"0\":{\"c\":0,\"t\":\"No Signal\"},\"1\":{\"c\":0,\"t\":\"Poor\"},\"2\":{\"c\":0,\"t\":\"Average\"},\"3\":{\"c\":0,\"t\":\"Good\"},\"4\":{\"c\":0,\"t\":\"Very Good\"},\"5\":{\"c\":0,\"t\":\"Excellent\"}},\"cm\":1,\"mu\":0,\"pos\":19,\"show_time\":false,\"text_params\":0,\"timeout\":0,\"uct\":0}","vt":0,"vs":0,"tbl":[],"itemId":16141282,"callMode":"create"}},{"svc":"unit/update_sensor","params":{"id":20,"n":"Idle Time","t":"custom","d":"","m":"hrs","p":"none","f":0,"c":"{\"act\":1,\"appear_in_popup\":false,\"ci\":{},\"cm\":0,\"mu\":0,\"pos\":7,\"show_time\":false,\"text_params\":0,\"timeout\":0,\"uct\":0}","vt":0,"vs":0,"tbl":[],"itemId":16141282,"callMode":"create"}},{"svc":"unit/update_sensor","params":{"id":21,"n":"SOS","t":"alarm trigger","d":"","m":"","p":"io_3","f":0,"c":"{\"act\":1,\"appear_in_popup\":true,\"ci\":{},\"cm\":0,\"mu\":0,\"pos\":21,\"show_time\":false,\"timeout\":0,\"uct\":0,\"unbound_code\":\"\",\"validate_driver_unbound\":0}","vt":1,"vs":0,"tbl":[],"itemId":16141282,"callMode":"create"}},{"svc":"item/update_custom_property","params":{"itemId":16141282,"name":"img_rot","value":"0"}},{"svc":"item/update_custom_property","params":{"itemId":16141282,"name":"label_color","value":"16711680"}},{"svc":"item/update_custom_property","params":{"itemId":16141282,"name":"monitoring_sensor_id","value":"0"}},{"svc":"item/update_custom_property","params":{"itemId":16141282,"name":"motion_state_sensor_id","value":"0"}},{"svc":"item/update_custom_property","params":{"itemId":16141282,"name":"solid_colors","value":"255"}},{"svc":"item/update_custom_property","params":{"itemId":16141282,"name":"track_solid","value":"255"}},{"svc":"item/update_custom_property","params":{"itemId":16141282,"name":"use_sensor_color","value":"0"}},{"svc":"unit/update_trip_detector","params":{"type":1,"gpsCorrection":1,"minSat":3,"minMovingSpeed":1,"minStayTime":300,"maxMessagesDistance":100000,"minTripTime":10,"minTripDistance":200,"itemId":16141282}},{"svc":"unit/update_fuel_calc_types","params":{"itemId":16141282,"calcTypes":0}},{"svc":"unit/update_fuel_level_params","params":{"flags":0,"ignoreStayTimeout":10,"minFillingVolume":20,"minTheftTimeout":0,"minTheftVolume":10,"filterQuality":0,"fillingsJoinInterval":300,"theftsJoinInterval":300,"extraFillingTimeout":0,"itemId":16141282}},{"svc":"unit/update_fuel_math_params","params":{"idling":2,"urban":10,"suburban":7,"itemId":16141282}},{"svc":"unit/update_fuel_rates_params","params":{"consSummer":10,"consWinter":12,"winterMonthFrom":11,"winterDayFrom":1,"winterMonthTo":1,"winterDayTo":30,"itemId":16141282}},{"svc":"unit/update_fuel_impulse_params","params":{"maxImpulses":0,"skipZero":0,"itemId":16141282}},{"svc":"unit/update_command_definition","params":{"id":0,"n":"Starter Disable","c":"custom_msg","l":"vrt","p":"setdigout1 0","a":16777216,"f":0,"itemId":16141282,"callMode":"create"}},{"svc":"unit/update_command_definition","params":{"id":0,"n":"Starter Enable","c":"custom_msg","l":"vrt","p":"setdigout1 1","a":16777216,"f":0,"itemId":16141282,"callMode":"create"}},{"svc":"unit/update_report_settings","params":{"itemId":16141282,"params":{"speedLimit":0,"maxMessagesInterval":0,"dailyEngineHoursRate":0,"urbanMaxSpeed":60,"mileageCoefficient":1,"fuelRateCoefficient":0,"speedingTolerance":5,"speedingMinDuration":1,"speedingMode":1}}},{"svc":"unit/update_activity_settings","params":{"type":1,"itemId":16141282}},{"svc":"unit/update_messages_filter","params":{"enabled":0,"skipInvalid":0,"minSats":0,"maxHdop":2,"maxSpeed":0,"lbsCorrection":0,"itemId":16141282}},{"svc":"unit/update_drive_rank_settings","params":{"itemId":16141282,"driveRank":{"global":{"accel_mode":"0"}}}}],"flags":"0"}&sid=04c20f654e27911fdad3dabf043bec96

2

Getting Error 4 when calling from C# HttpClient

Re: Getting Error 4 when calling from C# HttpClient

You are using HttpUtility.UrlEncode for whole string, that contains ampersand (&) inside JSON ("Network ID & Channel").

Looks like C# interprets it as params-delimiter and not encodes it.

Try to use Uri.EscapeDataString for params argument, or FormUrlEncodedContent for all of them.

3

Getting Error 4 when calling from C# HttpClient

Re: Getting Error 4 when calling from C# HttpClient

Thanks Rual

I already tried Uri.EscapeDataString - Same issue persists

FormUrlEncodedContent - We initially used  FormUrlEncodedContent and it was working fine but it has no of character limitations, due to that most of my calls were failed earlier and we changed to StringContent and now caught with & issue

I mean we spent the whole day yesterday with all combinations of different encoding like UTF8, ASCII, FormUrlEncode etc and nothing is working with StringContent.

Is it a a limitation of Wialon API not properly handling & when using StringContent in C# ??? pretty concerning issue this is for us.

Will appreciate any help on this regard.

4

Getting Error 4 when calling from C# HttpClient

Re: Getting Error 4 when calling from C# HttpClient

karthik, https://stackoverflow.com/questions/561 … ery-string

Head of Wialon Local Department
Gurtam
5

Getting Error 4 when calling from C# HttpClient

Re: Getting Error 4 when calling from C# HttpClient

> To create a new topic, you must have replied to at least 3 different topics.

Sorry, sorry, sorry. This should be the last one. Maybe admins should create 3 topics named "Fake topic to reply to #1(2, 3)" and pin them to the top of the list? sad I feel like a bad person now.