代码之家  ›  专栏  ›  技术社区  ›  MindSwipe

C#HttpClient Post不返回内容,但Postman返回

  •  0
  • MindSwipe  · 技术社区  · 7 年前

    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
    响应消息是 200
    下面是我发送的序列化XML数据,我从流中读取的方式与读取它以将其发送到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                               &#xD;</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”)

    1 回复  |  直到 7 年前
        1
  •  0
  •   MindSwipe    7 年前


    client.PostAsync(...) 我告诉API我正在发布 Encoding.UTF8 但我的序列化程序正在生成 ibm850