0

I am currently migrating .NetFramework4.8 project to .Net6. Here we have use case get some data from a old server. Below is the CURL response of the url from command prompt,

DATAFOUND 200 OK
Server: IMRIP DATA Provider 4.7
Content-Type: text/plain
Content-Length: 17798
Date: Thu, 20 Oct 2022 10:29:25 UTC

DTR;AB-LM-SSS;SS-VMR-Nx;CMRx;1(1)................etc..

But while calling the URL using HttpClient, we are getting below error due to the not well formed status code.

Received an invalid status line: 'DATAFOUND 200 OK'.

Same code is successfully getting executed in .NetFramework 4.8. In Net4.8, we have below config which can read unsafe headers from the response.

<system.net>
<settings>
  <servicePointManager expect100Continue="false" />
  <httpWebRequest useUnsafeHeaderParsing="true" />
</settings>
</system.net>

The code I am trying to execute is below,

var client = new HttpClient();
client.DefaultRequestHeaders.ExpectContinue = false;
client.DefaultRequestHeaders.Add("User-Agent", "Data provider");
client.DefaultRequestHeaders
      .Accept
      .Add(new MediaTypeWithQualityHeaderValue("text/plain"));
var response = await client.GetAsync("http://www.someurl.com:1102"); //Getting error here

Can you please help me to get equivalent config or method in .NET6 to solve this.

Power Star
  • 1,724
  • 2
  • 13
  • 25
  • Can you show your code that is calling `httpClient.GetAsync` and where in the line you get the error – rjs123431 Oct 20 '22 at 15:59
  • You may be able to get around this by using a `SocketsHttpHandler` and using the `PlaintextStreamFilter` method to modify the incoming stream. But you should really get that server fixed to return valid data. – DavidG Oct 20 '22 at 16:15
  • 200 OK is not an Error. It is the correct results. "Expect 100" means you are using HTTP 1.1 (not HTTP 1.0). HTTP 1.0 is stream mode and HTTP 1.1 is chunk mode. When using chunk mode you need to send a continue to get more data. You need to set the protocol version in client to 1.0. See : https://stackoverflow.com/questions/28097457/set-http-protocol-version-in-httpclient?force_isolation=true – jdweng Oct 20 '22 at 17:06
  • @jdweng Did you even read the question? `DATAFOUND 200 OK` is *not* a valid response. – DavidG Oct 20 '22 at 17:25

1 Answers1

0

.NET6 is follows official specs much more than older versions. Your server doesn't follow the official HTTP specs (https://developer.mozilla.org/en-US/docs/Web/HTTP/Overview#http_messages) and therefore it's unlikely to work.

Paraphrased from the link above:

The first line of a response contains 3 words:

  • Protocol version (e.g. HTTP/2)
  • Status code (e.g. 200)
  • Status message (e.g. OK)

DATAFOUND is not an HTTP protocol, therefore it is rejected.

If you look at the .NET6 source code and search for HttpProtocol, you will see how the different versions of HTTP are parsed. Most of that code is internal so it's not easy to add your own special version.

Neil
  • 11,059
  • 3
  • 31
  • 56