【发布时间】:2018-10-09 08:35:39
【问题描述】:
我正在尝试通过 POST 方法从 WebAPI 获取一些数据。 API 是一个公共传输数据 API,用于发送/接收 XML 序列化数据。当我从 Postman 调用 API 时,我得到了数据,当我尝试从 C# 做同样的事情时,虽然我收到一条状态码为 200 的消息,ContentLength 是-1,我无法从中读取任何数据。
我的 C# API 调用和流生成:
var stream = new MemoryStream();
serializer.Serialize(stream, toSend);
HttpClientHandler handler = new HttpClientHandler()
{
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
};
HttpClient client = new HttpClient(handler);
HttpResponseMessage response = await
client.PostAsync("https://api.opentransportdata.swiss/trias",
new StringContent(new StreamReader(stream,
Encoding.UTF8).ReadToEnd(), Encoding.UTF8, "text/xml"));
我的stream 是一个MemoryStream,其中包含我的 XML 序列化数据。
响应消息为200(OK),但内容为空,当我对 Postman 执行相同操作时,它可以工作。
这是我发送的序列化 XML 数据,我从 Stream 中读取,就像我读取它以将其发送到 API 一样
<?xml version="1.0" encoding="utf-16"?>
<Trias xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" version="1.1" xmlns="http://www.vdv.de/trias">
<ServiceRequest>
<siri_x003A_RequestTimestamp>2018-10-09T10:31:56.2886025+02:00</siri_x003A_RequestTimestamp>
<siri_x003A_RequestorRef>SEUS</siri_x003A_RequestorRef>
<RequestPayload>
<TripRequest>
<Origin>
<LocationRef>
<StopPointRef>8500320</StopPointRef>
</LocationRef>
</Origin>
<Destination>
<LocationRef>
<StopPointRef>8500322</StopPointRef>
</LocationRef>
</Destination>
</TripRequest>
<Params>
<NumberOfResults>0</NumberOfResults>
<IncludeTrackSections>false</IncludeTrackSections>
<IncludeLegProjection>false</IncludeLegProjection>
<IncludeIntermediateStops>false</IncludeIntermediateStops>
</Params>
</RequestPayload>
</ServiceRequest>
</Trias>
这是预期的结果:
<?xml version="1.0" encoding="UTF-8"?>
<Trias xmlns="http://www.vdv.de/trias" version="1.1">
<ServiceDelivery>
<ResponseTimestamp xmlns="http://www.siri.org.uk/siri">2018-10-09T08:33:26Z</ResponseTimestamp>
<ProducerRef xmlns="http://www.siri.org.uk/siri">EFAController10.2.9.62-WIN-G0NJHFUK71P</ProducerRef>
<Status xmlns="http://www.siri.org.uk/siri">true</Status>
<MoreData>false</MoreData>
<Language>de</Language>
<DeliveryPayload>
<TripResponse>
<TripResult>
<ResultId>ID-A24DE912-0263-4067-AD73-591DF01F4B05</ResultId>
<Trip>
<TripId>ID-E442EC5E-1B70-4E9D-836F-47585406D2F7</TripId>
<Duration>PT7M</Duration>
<StartTime>2018-10-09T08:19:00Z</StartTime>
<EndTime>2018-10-09T08:26:00Z</EndTime>
<Interchanges>0</Interchanges>
<TripLeg>
<LegId>1</LegId>
<TimedLeg>
<LegBoard>
<StopPointRef>8500320</StopPointRef>
<StopPointName>
<Text>Stein-Säckingen</Text>
<Language>de</Language>
</StopPointName>
<PlannedBay>
<Text>2</Text>
<Language>de</Language>
</PlannedBay>
<ServiceDeparture>
<TimetabledTime>2018-10-09T08:19:00Z</TimetabledTime>
</ServiceDeparture>
<StopSeqNumber>1</StopSeqNumber>
</LegBoard>
<LegAlight>
<StopPointRef>8500322</StopPointRef>
<StopPointName>
<Text>Laufenburg</Text>
<Language>de</Language>
</StopPointName>
<PlannedBay>
<Text>1</Text>
<Language>de</Language>
</PlannedBay>
<ServiceArrival>
<TimetabledTime>2018-10-09T08:26:00Z</TimetabledTime>
</ServiceArrival>
<StopSeqNumber>2</StopSeqNumber>
</LegAlight>
<Service>
<OperatingDayRef>2018-10-09</OperatingDayRef>
<JourneyRef>odp:04001::H:j18:17235:17235</JourneyRef>
<LineRef>odp:04001::H</LineRef>
<DirectionRef>outward</DirectionRef>
<Mode>
<PtMode>rail</PtMode>
<RailSubmode>suburbanRailway</RailSubmode>
<Name>
<Text>S-Bahn</Text>
<Language>de</Language>
</Name>
</Mode>
<PublishedLineName>
<Text>1</Text>
<Language>de</Language>
</PublishedLineName>
<OperatorRef>odp:11</OperatorRef>
<OriginText>
<Text></Text>
<Language>de</Language>
</OriginText>
<DestinationStopPointRef>8500322</DestinationStopPointRef>
<DestinationText>
<Text>Laufenburg</Text>
<Language>de</Language>
</DestinationText>
</Service>
</TimedLeg>
</TripLeg>
</Trip>
</TripResult>
<TripResult>
<ResultId>ID-CE13C278-DFBE-4AFF-8318-1E96EEE6FA9B</ResultId>
<Trip>
<TripId>ID-832873CC-4085-4131-9265-D5EF1F80DBF6</TripId>
<Duration>PT25M</Duration>
<StartTime>2018-10-09T08:35:00Z</StartTime>
<EndTime>2018-10-09T09:00:00Z</EndTime>
<Interchanges>0</Interchanges>
<TripLeg>
<LegId>1</LegId>
<InterchangeLeg>
<InterchangeMode>walk</InterchangeMode>
<LegStart>
<StopPointRef>8500320</StopPointRef>
<LocationName>
<Text>Stein-Säckingen</Text>
<Language>de</Language>
</LocationName>
</LegStart>
<LegEnd>
<StopPointRef>8572748</StopPointRef>
<LocationName>
<Text>Stein-Säckingen, Bahnhof</Text>
<Language>de</Language>
</LocationName>
</LegEnd>
<TimeWindowStart>2018-10-09T08:35:00Z</TimeWindowStart>
<TimeWindowEnd>2018-10-09T08:39:00Z</TimeWindowEnd>
<Duration>PT4M</Duration>
</InterchangeLeg>
</TripLeg>
<TripLeg>
<LegId>2</LegId>
<TimedLeg>
<LegBoard>
<StopPointRef>8572748</StopPointRef>
<StopPointName>
<Text>Stein-Säckingen, Bahnhof</Text>
<Language>de</Language>
</StopPointName>
<ServiceDeparture>
<TimetabledTime>2018-10-09T08:39:00Z</TimetabledTime>
</ServiceDeparture>
<StopSeqNumber>1</StopSeqNumber>
</LegBoard>
<LegAlight>
<StopPointRef>8572403</StopPointRef>
<StopPointName>
<Text>Laufenburg, Bahnhof</Text>
<Language>de</Language>
</StopPointName>
<ServiceArrival>
<TimetabledTime>2018-10-09T08:56:00Z</TimetabledTime>
</ServiceArrival>
<StopSeqNumber>10</StopSeqNumber>
</LegAlight>
<Service>
<OperatingDayRef>2018-10-09</OperatingDayRef>
<JourneyRef>odp:01143::R:j18:14332:14332</JourneyRef>
<LineRef>odp:01143::R</LineRef>
<DirectionRef>return</DirectionRef>
<Mode>
<PtMode>bus</PtMode>
<BusSubmode>regionalBus</BusSubmode>
<Name>
<Text>Bus</Text>
<Language>de</Language>
</Name>
</Mode>
<PublishedLineName>
<Text>143</Text>
<Language>de</Language>
</PublishedLineName>
<OperatorRef>odp:801</OperatorRef>
<Attribute>
<Text>
<Text>Linie 143: 1555</Text>
<Language>de</Language>
</Text>
<Code>Y3933</Code>
<Mandatory>false</Mandatory>
</Attribute>
<OriginText>
<Text></Text>
<Language>de</Language>
</OriginText>
<DestinationText>
<Text>Laufenburg, Bahnhof 
</Text>
<Language>de</Language>
</DestinationText>
</Service>
</TimedLeg>
</TripLeg>
<TripLeg>
<LegId>3</LegId>
<InterchangeLeg>
<InterchangeMode>walk</InterchangeMode>
<LegStart>
<StopPointRef>8572403</StopPointRef>
<LocationName>
<Text>Laufenburg, Bahnhof</Text>
<Language>de</Language>
</LocationName>
</LegStart>
<LegEnd>
<StopPointRef>8500322</StopPointRef>
<LocationName>
<Text>Laufenburg</Text>
<Language>de</Language>
</LocationName>
</LegEnd>
<TimeWindowStart>2018-10-09T08:56:00Z</TimeWindowStart>
<TimeWindowEnd>2018-10-09T09:00:00Z</TimeWindowEnd>
<Duration>PT4M</Duration>
</InterchangeLeg>
</TripLeg>
</Trip>
</TripResult>
<TripResult>
<ResultId>ID-7C9B4974-1D9D-4828-98BB-5A7022CD55B1</ResultId>
<Trip>
<TripId>ID-2FB9793D-93E8-495D-9802-0ECC6C4B98F3</TripId>
<Duration>PT6M</Duration>
<StartTime>2018-10-09T09:19:00Z</StartTime>
<EndTime>2018-10-09T09:25:00Z</EndTime>
<Interchanges>0</Interchanges>
<TripLeg>
<LegId>1</LegId>
<TimedLeg>
<LegBoard>
<StopPointRef>8500320</StopPointRef>
<StopPointName>
<Text>Stein-Säckingen</Text>
<Language>de</Language>
</StopPointName>
<PlannedBay>
<Text>2</Text>
<Language>de</Language>
</PlannedBay>
<ServiceDeparture>
<TimetabledTime>2018-10-09T09:19:00Z</TimetabledTime>
<EstimatedTime>2018-10-09T09:19:00Z</EstimatedTime>
</ServiceDeparture>
<StopSeqNumber>1</StopSeqNumber>
</LegBoard>
<LegAlight>
<StopPointRef>8500322</StopPointRef>
<StopPointName>
<Text>Laufenburg</Text>
<Language>de</Language>
</StopPointName>
<PlannedBay>
<Text>1</Text>
<Language>de</Language>
</PlannedBay>
<ServiceArrival>
<TimetabledTime>2018-10-09T09:26:00Z</TimetabledTime>
<EstimatedTime>2018-10-09T09:25:00Z</EstimatedTime>
</ServiceArrival>
<StopSeqNumber>2</StopSeqNumber>
</LegAlight>
<Service>
<OperatingDayRef>2018-10-09</OperatingDayRef>
<JourneyRef>odp:04001::H:j18:17239:17239</JourneyRef>
<LineRef>odp:04001::H</LineRef>
<DirectionRef>outward</DirectionRef>
<Mode>
<PtMode>rail</PtMode>
<RailSubmode>suburbanRailway</RailSubmode>
<Name>
<Text>S-Bahn</Text>
<Language>de</Language>
</Name>
</Mode>
<PublishedLineName>
<Text>1</Text>
<Language>de</Language>
</PublishedLineName>
<OperatorRef>odp:11</OperatorRef>
<OriginText>
<Text></Text>
<Language>de</Language>
</OriginText>
<DestinationStopPointRef>8500322</DestinationStopPointRef>
<DestinationText>
<Text>Laufenburg</Text>
<Language>de</Language>
</DestinationText>
</Service>
</TimedLeg>
</TripLeg>
</Trip>
</TripResult>
</TripResponse>
</DeliveryPayload>
</ServiceDelivery>
</Trias>
您可以自己尝试一下here(只需将“模板”更改为“TripRequest”)
【问题讨论】:
-
能否请您添加您的所有 API 调用代码,以便我们查看您如何生成
stream -
@ershoaib 更新了代码块 ;)
-
解决此类问题的最佳方法是使用像wireshark或fiddler这样的嗅探器。然后将 Postman 结果与您的代码进行比较。通常解决方案是将缺失的标头添加到 HttpClient 中。 httpClient 不会像其他 http 方法那样自动添加请求头。
-
@jdweng 我也尝试使用
HttpWebRequest自动添加所需的标题。而且我还手动将标头添加到 API 调用中,但仍然无法正常工作。据我所知,邮递员也没有添加任何标题。HttpClient有一个 Authorization 标头,PostAsync方法还添加了Content-Type text/xml标头 -
邮递员怎么称呼它,请出示邮递员截图
标签: c# xml api post xml-serialization