C# HttpClient Post returns 没有内容但是 Postman 有
C# HttpClient Post returns no content but Postman does
我正在尝试通过 POST 方法从 WebAPI 获取一些数据。 API 是一个 public 传输数据 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
是一个包含我的 XML 序列化数据的 MemoryStream
。
响应消息是 200
(好的)但是内容是空的,当我用 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(只需将 "Template" 更改为 "TripRequest")
我想通了。
在 client.PostAsync(...)
中,我告诉 API 我正在发布 Encoding.UTF8
编码数据,但我的序列化器正在生成 ibm850
编码数据
我正在尝试通过 POST 方法从 WebAPI 获取一些数据。 API 是一个 public 传输数据 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
是一个包含我的 XML 序列化数据的 MemoryStream
。
响应消息是 200
(好的)但是内容是空的,当我用 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(只需将 "Template" 更改为 "TripRequest")
我想通了。
在 client.PostAsync(...)
中,我告诉 API 我正在发布 Encoding.UTF8
编码数据,但我的序列化器正在生成 ibm850
编码数据