C#代码:
SamplePortTypeClient server = new SamplePortTypeClient(); AvailabilityRequest availabilityRequest = new AvailabilityRequest(); availabilityRequest.UserId = "TEAMETRO"; availabilityRequest.Password = "TEAMETRO"; availabilityRequest.Token = "12345"; AvailabilityRequestAvailabilityCriteria rqAC = new AvailabilityRequestAvailabilityCriteria(); rqAC.AvailableOnly = false; rqAC.SummaryOnly = false; rqAC.PlanPerHotel = 100; rqAC.SortOrder = "C"; rqAC.Limit = 1; rqAC.Offset = 1; AvailabilityRequestAvailabilityCriteriaDateRange rqACD = new AvailabilityRequestAvailabilityCriteriaDateRange(); rqACD.Start = Convert.ToDateTime("2017-05-16"); rqACD.End = Convert.ToDateTime("2017-05-20"); rqAC.DateRange = rqACD; AvailabilityRequestAvailabilityCriteriaAvailability rqACA = new AvailabilityRequestAvailabilityCriteriaAvailability(); rqACA.StateCode = "13"; rqACA.HotelCode = "3628-302";//1338-103 rqAC.Availability = rqACA; availabilityRequest.AvailabilityCriteria = rqAC; AvailabilityResponse availabilityResponse = server.AvailabilityRequest(availabilityRequest); string json = JsonConvert.SerializeObject(availabilityResponse, Formatting.Indented); System.Diagnostics.Debug.WriteLine(json); System.Console.ReadLine();
返回结果:
1 { 2 "Error": null, 3 "RequestParameters": null, 4 "Availabilities": { 5 "Availability": [ 6 { 7 "HotelName": "Shijuku New City Hotel", 8 "RoomRates": [ 9 { 10 "RoomTypeName": "Twin", 11 "RatePlanName": "Standard-Room only", 12 "Restriction": { 13 "MinGuestCount": 1, 14 "MinGuestCountSpecified": true, 15 "MaxGuestCount": 2, 16 "MaxGuestCountSpecified": true, 17 "CutOff": 3, 18 "CutOffSpecified": true, 19 "MinLengthOfStay": 1, 20 "MinLengthOfStaySpecified": true, 21 "MaxLengthOfStay": 13, 22 "MaxLengthOfStaySpecified": true 23 }, 24 "DailyRates": [ 25 { 26 "Rates": { 27 "Rate": [ 28 { 29 "AgeCategory": "AD", 30 "GuestCount": 1, 31 "GuestCountSpecified": true, 32 "Amount": 11100, 33 "AmountSpecified": true 34 }, 35 { 36 "AgeCategory": "AD", 37 "GuestCount": 2, 38 "GuestCountSpecified": true, 39 "Amount": 11100, 40 "AmountSpecified": true 41 }, 42 { 43 "AgeCategory": "CA", 44 "GuestCount": 2, 45 "GuestCountSpecified": true, 46 "Amount": 11100, 47 "AmountSpecified": true 48 } 49 ], 50 "CurrencyCode": "JPY" 51 }, 52 "AvailableRooms": { 53 "NumberOfUnits": 79, 54 "NumberOfUnitsSpecified": true 55 }, 56 "Date": "2017-05-22T00:00:00", 57 "DateSpecified": true 58 } 59 ], 60 "RoomTypeCode": "19T102", 61 "RatePlanCode": "WA0000535", 62 "MealTypeCode": "01" 63 } 64 ], 65 "StateCode": "13", 66 "CityCode": "191", 67 "HotelCode": "3628-302" 68 } 69 ], 70 "TotalCount": "1", 71 "ResultCount": "1", 72 "ResultPerPage": "1", 73 "ResultPage": "1" 74 }, 75 "Token": "12345", 76 "Status": "Success", 77 "TimeStamp": "2017-05-17T15:10:40.4+08:00", 78 "TimeStampSpecified": true 79 }
Java 代码
java
1 package com.webservice.test; 2 3 import java.io.StringWriter; 4 import java.io.Writer; 5 import java.rmi.RemoteException; 6 import java.text.ParseException; 7 import java.text.SimpleDateFormat; 8 9 import com.fasterxml.jackson.core.JsonProcessingException; 10 import com.fasterxml.jackson.databind.ObjectMapper; 11 import com.fasterxml.jackson.databind.SerializationFeature; 12 13 import jp.co.nta.wdinbd.request.AvailabilityRequest; 14 import jp.co.nta.wdinbd.request.AvailabilityRequestAvailabilityCriteria; 15 import jp.co.nta.wdinbd.request.AvailabilityRequestAvailabilityCriteriaAvailability; 16 import jp.co.nta.wdinbd.request.AvailabilityRequestAvailabilityCriteriaDateRange; 17 import jp.co.nta.wdinbd.response.AvailabilityResponse; 18 import jp.co.nta.wdinbd.service.SamplePortType; 19 import jp.co.nta.wdinbd.service.SamplePortTypeProxy; 20 21 public class WbCall { 22 private static ObjectMapper objectMapper = new ObjectMapper(); 23 24 public static void main(String[] args) throws RemoteException, ParseException, JsonProcessingException { 25 // TODO Auto-generated method stub 26 // NTAInboundWebservice service= new NTAInboundWebserviceSoapStub(); 27 SamplePortType samplePortType = new SamplePortTypeProxy(); 28 AvailabilityRequest availabilityRequest = new AvailabilityRequest(); 29 availabilityRequest.setUserId("TEAMETRO"); 30 availabilityRequest.setPassword("TEAMETRO"); 31 availabilityRequest.setToken("12345"); 32 33 AvailabilityRequestAvailabilityCriteria availabilityCriteria = new AvailabilityRequestAvailabilityCriteria(); 34 availabilityCriteria.setAvailableOnly(false); 35 availabilityCriteria.setSummaryOnly(false); 36 availabilityCriteria.setPlanPerHotel(100); 37 availabilityCriteria.setSortOrder("C"); 38 availabilityCriteria.setLimit(1); 39 availabilityCriteria.setOffset(1); 40 availabilityRequest.setAvailabilityCriteria(availabilityCriteria); 41 42 AvailabilityRequestAvailabilityCriteriaDateRange availabilityCriteriaDateRange = new AvailabilityRequestAvailabilityCriteriaDateRange(); 43 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 44 availabilityCriteriaDateRange.setStart(sdf.parse("2017-05-18")); 45 availabilityCriteriaDateRange.setEnd(sdf.parse("2017-05-20")); 46 availabilityCriteria.setDateRange(availabilityCriteriaDateRange); 47 48 AvailabilityRequestAvailabilityCriteriaAvailability rqACA = new AvailabilityRequestAvailabilityCriteriaAvailability(); 49 rqACA.setStateCode("13"); 50 rqACA.setHotelCode("3628-302");// 1338-103 51 availabilityCriteria.setAvailability(rqACA); 52 53 AvailabilityResponse availabilityResponse = samplePortType.availabilityRequest(availabilityRequest); 54 Writer write = new StringWriter(); 55 56 objectMapper.setDateFormat(sdf); 57 objectMapper.enable(SerializationFeature.INDENT_OUTPUT); 58 String json = objectMapper.writeValueAsString(availabilityResponse); 59 System.out.println(json); 60 //System.out.println(availabilityResponse); 61 } 62 63 }
结果
1 { 2 "error" : null, 3 "requestParameters" : { 4 "availabilityCriteria" : { 5 "availability" : { 6 "stateCode" : "13", 7 "cityCode" : null, 8 "hotelCode" : "3628-302" 9 }, 10 "dateRange" : { 11 "start" : "2017-05-18", 12 "end" : "2017-05-20" 13 }, 14 "candidates" : { 15 "mealTypeCode" : null, 16 "roomKind" : null, 17 "availableRooms" : 1, 18 "adultCount" : 0, 19 "childCount" : 0 20 }, 21 "availableOnly" : false, 22 "summaryOnly" : false, 23 "planPerHotel" : 100, 24 "sortOrder" : "C", 25 "limit" : 1, 26 "offset" : 1 27 } 28 }, 29 "availabilities" : [ { 30 "hotelName" : "Shijuku New City Hotel", 31 "roomRates" : [ { 32 "roomTypeName" : "Twin", 33 "ratePlanName" : "Standard-Room only", 34 "restriction" : { 35 "minGuestCount" : 1, 36 "maxGuestCount" : 2, 37 "cutOff" : 3, 38 "minLengthOfStay" : 1, 39 "maxLengthOfStay" : 13 40 }, 41 "dailyRates" : [ { 42 "rates" : null, 43 "availableRooms" : { 44 "numberOfUnits" : 0 45 }, 46 "date" : "2017-05-18" 47 }, { 48 "rates" : null, 49 "availableRooms" : { 50 "numberOfUnits" : 0 51 }, 52 "date" : "2017-05-19" 53 }, { 54 "rates" : [ { 55 "ageCategory" : "AD", 56 "guestCount" : 1, 57 "amount" : 13300 58 }, { 59 "ageCategory" : "AD", 60 "guestCount" : 2, 61 "amount" : 13300 62 }, { 63 "ageCategory" : "CA", 64 "guestCount" : 2, 65 "amount" : 13300 66 } ], 67 "availableRooms" : { 68 "numberOfUnits" : 100 69 }, 70 "date" : "2017-05-20" 71 } ], 72 "roomTypeCode" : "19T102", 73 "ratePlanCode" : "WA0000535", 74 "mealTypeCode" : "01" 75 }, { 76 "roomTypeName" : "Twin", 77 "ratePlanName" : "DerbySoft TEST Standard No Meals", 78 "restriction" : { 79 "minGuestCount" : 1, 80 "maxGuestCount" : 2, 81 "cutOff" : 5, 82 "minLengthOfStay" : 1, 83 "maxLengthOfStay" : 13 84 }, 85 "dailyRates" : [ { 86 "rates" : null, 87 "availableRooms" : { 88 "numberOfUnits" : 0 89 }, 90 "date" : "2017-05-18" 91 }, { 92 "rates" : null, 93 "availableRooms" : { 94 "numberOfUnits" : 0 95 }, 96 "date" : "2017-05-19" 97 }, { 98 "rates" : null, 99 "availableRooms" : { 100 "numberOfUnits" : 0 101 }, 102 "date" : "2017-05-20" 103 } ], 104 "roomTypeCode" : "19T102", 105 "ratePlanCode" : "WA0867131", 106 "mealTypeCode" : "01" 107 }, { 108 "roomTypeName" : "Twin", 109 "ratePlanName" : "DerbySoft TEST Book more than 3 nights with Special Discount-No Meals", 110 "restriction" : { 111 "minGuestCount" : 1, 112 "maxGuestCount" : 2, 113 "cutOff" : 5, 114 "minLengthOfStay" : 3, 115 "maxLengthOfStay" : 13 116 }, 117 "dailyRates" : [ { 118 "rates" : null, 119 "availableRooms" : { 120 "numberOfUnits" : 0 121 }, 122 "date" : "2017-05-18" 123 }, { 124 "rates" : null, 125 "availableRooms" : { 126 "numberOfUnits" : 0 127 }, 128 "date" : "2017-05-19" 129 }, { 130 "rates" : null, 131 "availableRooms" : { 132 "numberOfUnits" : 0 133 }, 134 "date" : "2017-05-20" 135 } ], 136 "roomTypeCode" : "19T102", 137 "ratePlanCode" : "WA0867132", 138 "mealTypeCode" : "01" 139 }, { 140 "roomTypeName" : "Twin", 141 "ratePlanName" : "DerbySoft TEST Early Bird Discount 41 Days-No Meals", 142 "restriction" : { 143 "minGuestCount" : 1, 144 "maxGuestCount" : 2, 145 "cutOff" : 40, 146 "minLengthOfStay" : 1, 147 "maxLengthOfStay" : 13 148 }, 149 "dailyRates" : [ { 150 "rates" : null, 151 "availableRooms" : { 152 "numberOfUnits" : 0 153 }, 154 "date" : "2017-05-18" 155 }, { 156 "rates" : null, 157 "availableRooms" : { 158 "numberOfUnits" : 0 159 }, 160 "date" : "2017-05-19" 161 }, { 162 "rates" : null, 163 "availableRooms" : { 164 "numberOfUnits" : 0 165 }, 166 "date" : "2017-05-20" 167 } ], 168 "roomTypeCode" : "19T102", 169 "ratePlanCode" : "WA0867133", 170 "mealTypeCode" : "01" 171 }, { 172 "roomTypeName" : "Twin", 173 "ratePlanName" : "DerbySoft TEST Standard Breakfast Incl", 174 "restriction" : { 175 "minGuestCount" : 1, 176 "maxGuestCount" : 2, 177 "cutOff" : 5, 178 "minLengthOfStay" : 1, 179 "maxLengthOfStay" : 13 180 }, 181 "dailyRates" : [ { 182 "rates" : null, 183 "availableRooms" : { 184 "numberOfUnits" : 0 185 }, 186 "date" : "2017-05-18" 187 }, { 188 "rates" : null, 189 "availableRooms" : { 190 "numberOfUnits" : 0 191 }, 192 "date" : "2017-05-19" 193 }, { 194 "rates" : null, 195 "availableRooms" : { 196 "numberOfUnits" : 0 197 }, 198 "date" : "2017-05-20" 199 } ], 200 "roomTypeCode" : "19T102", 201 "ratePlanCode" : "WA0867134", 202 "mealTypeCode" : "03" 203 }, { 204 "roomTypeName" : "Triple", 205 "ratePlanName" : "DerbySoft TEST Standard No Meals", 206 "restriction" : { 207 "minGuestCount" : 1, 208 "maxGuestCount" : 3, 209 "cutOff" : 5, 210 "minLengthOfStay" : 1, 211 "maxLengthOfStay" : 13 212 }, 213 "dailyRates" : [ { 214 "rates" : null, 215 "availableRooms" : { 216 "numberOfUnits" : 0 217 }, 218 "date" : "2017-05-18" 219 }, { 220 "rates" : null, 221 "availableRooms" : { 222 "numberOfUnits" : 0 223 }, 224 "date" : "2017-05-19" 225 }, { 226 "rates" : null, 227 "availableRooms" : { 228 "numberOfUnits" : 0 229 }, 230 "date" : "2017-05-20" 231 } ], 232 "roomTypeCode" : "19T203", 233 "ratePlanCode" : "WA0867131", 234 "mealTypeCode" : "01" 235 }, { 236 "roomTypeName" : "Triple", 237 "ratePlanName" : "DerbySoft TEST Book more than 3 nights with Special Discount-No Meals", 238 "restriction" : { 239 "minGuestCount" : 1, 240 "maxGuestCount" : 3, 241 "cutOff" : 5, 242 "minLengthOfStay" : 3, 243 "maxLengthOfStay" : 13 244 }, 245 "dailyRates" : [ { 246 "rates" : null, 247 "availableRooms" : { 248 "numberOfUnits" : 0 249 }, 250 "date" : "2017-05-18" 251 }, { 252 "rates" : null, 253 "availableRooms" : { 254 "numberOfUnits" : 0 255 }, 256 "date" : "2017-05-19" 257 }, { 258 "rates" : null, 259 "availableRooms" : { 260 "numberOfUnits" : 0 261 }, 262 "date" : "2017-05-20" 263 } ], 264 "roomTypeCode" : "19T203", 265 "ratePlanCode" : "WA0867132", 266 "mealTypeCode" : "01" 267 }, { 268 "roomTypeName" : "Triple", 269 "ratePlanName" : "DerbySoft TEST Early Bird Discount 41 Days-No Meals", 270 "restriction" : { 271 "minGuestCount" : 1, 272 "maxGuestCount" : 3, 273 "cutOff" : 40, 274 "minLengthOfStay" : 1, 275 "maxLengthOfStay" : 13 276 }, 277 "dailyRates" : [ { 278 "rates" : null, 279 "availableRooms" : { 280 "numberOfUnits" : 0 281 }, 282 "date" : "2017-05-18" 283 }, { 284 "rates" : null, 285 "availableRooms" : { 286 "numberOfUnits" : 0 287 }, 288 "date" : "2017-05-19" 289 }, { 290 "rates" : null, 291 "availableRooms" : { 292 "numberOfUnits" : 0 293 }, 294 "date" : "2017-05-20" 295 } ], 296 "roomTypeCode" : "19T203", 297 "ratePlanCode" : "WA0867133", 298 "mealTypeCode" : "01" 299 }, { 300 "roomTypeName" : "Triple", 301 "ratePlanName" : "DerbySoft TEST Standard Breakfast Incl", 302 "restriction" : { 303 "minGuestCount" : 1, 304 "maxGuestCount" : 3, 305 "cutOff" : 5, 306 "minLengthOfStay" : 1, 307 "maxLengthOfStay" : 13 308 }, 309 "dailyRates" : [ { 310 "rates" : null, 311 "availableRooms" : { 312 "numberOfUnits" : 0 313 }, 314 "date" : "2017-05-18" 315 }, { 316 "rates" : null, 317 "availableRooms" : { 318 "numberOfUnits" : 0 319 }, 320 "date" : "2017-05-19" 321 }, { 322 "rates" : null, 323 "availableRooms" : { 324 "numberOfUnits" : 0 325 }, 326 "date" : "2017-05-20" 327 } ], 328 "roomTypeCode" : "19T203", 329 "ratePlanCode" : "WA0867134", 330 "mealTypeCode" : "03" 331 }, { 332 "roomTypeName" : "Single 1bed", 333 "ratePlanName" : "DerbySoft TEST Standard No Meals", 334 "restriction" : { 335 "minGuestCount" : 1, 336 "maxGuestCount" : 1, 337 "cutOff" : 5, 338 "minLengthOfStay" : 1, 339 "maxLengthOfStay" : 13 340 }, 341 "dailyRates" : [ { 342 "rates" : null, 343 "availableRooms" : { 344 "numberOfUnits" : 0 345 }, 346 "date" : "2017-05-18" 347 }, { 348 "rates" : null, 349 "availableRooms" : { 350 "numberOfUnits" : 0 351 }, 352 "date" : "2017-05-19" 353 }, { 354 "rates" : null, 355 "availableRooms" : { 356 "numberOfUnits" : 0 357 }, 358 "date" : "2017-05-20" 359 } ], 360 "roomTypeCode" : "19T301", 361 "ratePlanCode" : "WA0867131", 362 "mealTypeCode" : "01" 363 }, { 364 "roomTypeName" : "Single 1bed", 365 "ratePlanName" : "DerbySoft TEST Book more than 3 nights with Special Discount-No Meals", 366 "restriction" : { 367 "minGuestCount" : 1, 368 "maxGuestCount" : 1, 369 "cutOff" : 5, 370 "minLengthOfStay" : 3, 371 "maxLengthOfStay" : 13 372 }, 373 "dailyRates" : [ { 374 "rates" : null, 375 "availableRooms" : { 376 "numberOfUnits" : 0 377 }, 378 "date" : "2017-05-18" 379 }, { 380 "rates" : null, 381 "availableRooms" : { 382 "numberOfUnits" : 0 383 }, 384 "date" : "2017-05-19" 385 }, { 386 "rates" : null, 387 "availableRooms" : { 388 "numberOfUnits" : 0 389 }, 390 "date" : "2017-05-20" 391 } ], 392 "roomTypeCode" : "19T301", 393 "ratePlanCode" : "WA0867132", 394 "mealTypeCode" : "01" 395 }, { 396 "roomTypeName" : "Single 1bed", 397 "ratePlanName" : "DerbySoft TEST Early Bird Discount 41 Days-No Meals", 398 "restriction" : { 399 "minGuestCount" : 1, 400 "maxGuestCount" : 1, 401 "cutOff" : 40, 402 "minLengthOfStay" : 1, 403 "maxLengthOfStay" : 13 404 }, 405 "dailyRates" : [ { 406 "rates" : null, 407 "availableRooms" : { 408 "numberOfUnits" : 0 409 }, 410 "date" : "2017-05-18" 411 }, { 412 "rates" : null, 413 "availableRooms" : { 414 "numberOfUnits" : 0 415 }, 416 "date" : "2017-05-19" 417 }, { 418 "rates" : null, 419 "availableRooms" : { 420 "numberOfUnits" : 0 421 }, 422 "date" : "2017-05-20" 423 } ], 424 "roomTypeCode" : "19T301", 425 "ratePlanCode" : "WA0867133", 426 "mealTypeCode" : "01" 427 }, { 428 "roomTypeName" : "Single 1bed", 429 "ratePlanName" : "DerbySoft TEST Standard Breakfast Incl", 430 "restriction" : { 431 "minGuestCount" : 1, 432 "maxGuestCount" : 1, 433 "cutOff" : 5, 434 "minLengthOfStay" : 1, 435 "maxLengthOfStay" : 13 436 }, 437 "dailyRates" : [ { 438 "rates" : null, 439 "availableRooms" : { 440 "numberOfUnits" : 0 441 }, 442 "date" : "2017-05-18" 443 }, { 444 "rates" : null, 445 "availableRooms" : { 446 "numberOfUnits" : 0 447 }, 448 "date" : "2017-05-19" 449 }, { 450 "rates" : null, 451 "availableRooms" : { 452 "numberOfUnits" : 0 453 }, 454 "date" : "2017-05-20" 455 } ], 456 "roomTypeCode" : "19T301", 457 "ratePlanCode" : "WA0867134", 458 "mealTypeCode" : "03" 459 } ], 460 "stateCode" : "13", 461 "cityCode" : "191", 462 "hotelCode" : "3628-302" 463 } ], 464 "token" : "12345", 465 "status" : "Success", 466 "timeStamp" : "2017-05-17" 467 }
现在Java调用的结果是正确的,但是我费解的是 C#调用为什么结果会不一样,都是同样的参数。
原因出在,你用C# 把 类对象 序列成JSON数据传递过去的数据和Java把 类对象 序列化成json 传递过去的数据不一样。
序列化这里的问题
C#里面 我调用的是vs添加服务引用的对象,并没有自己序列化,难道是生成代码有问题?
@AlanCoder:我说的是这一句啊大哥
string json = JsonConvert.SerializeObject(availabilityResponse, Formatting.Indented);
你用C#提交的json数据和用java提交的不一样的
http抓个包看一下请求参数。就全明白了。几分钟的事情
抓了下包,看到我在代码里面赋值的字段有些丢了 ,有些传过去了,所以导致结果错误,但是我不明白的是为什么会丢。
@AlanCoder:
webservice如何生成的。server端是java的还是C#的。
@calvinK: server端 不知道是什么语言的,应该不是c#。我这边是c#直接引用服务调用的。
你需要的是看真正提交的HTTP数据,而不是看代码。
大叔 我抓下来 看post的数据 有些 有的字段没序列化 有些 整个对象都丢了 这有点不懂了
能指点下么? 我抓下来是这样的 ,可是 字段我都赋值了为什么会丢呢?
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; MS Web Services Client Protocol 2.0.50727.8784) VsDebuggerCausalityData: uIDPo+ypzVh7CFtLuU4B0frKBKUAAAAA/NZ72q8zfUiphYZBIRA3II3S/xysXFJPuLw1z9SUDQIACQAA Content-Type: text/xml; charset=utf-8 SOAPAction: "urn:AvailabilityRequest" Host: direct9.nta.co.jp Content-Length: 514 Expect: 100-continue Connection: Keep-Alive <?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><AvailabilityRequest Token="12345" UserId="TEAMETRO" Password="TEAMETRO" xmlns="http://wdinbd.nta.co.jp/request"><AvailabilityCriteria SortOrder="D" xmlns=""><Availability StateCode="13" HotelCode="3628-302" /><DateRange /></AvailabilityCriteria></AvailabilityRequest></soap:Body></soap:Envelope>HTTP/1.1 100 Continue
@AlanCoder: 封装那么严实鬼知道怎么丢失的。你直接用tcp过去肯定对。既然已经知道哪里错误就换个靠谱的库或者自己基于http或者tcp写就行了。