2

I'm using SolrExpress to search and index documents within c# (dotnet core). Inserting (indexing) documents works fine since this is a nice post request.

However when i'm trying to do a select query (to retrieve documents) i'm getting aggregation exceptions. By digging down the source in SolrExpress i came upon the following source:

private WebRequest Prepare(SecurityOptions options, string requestMethod, string handler, string data)
{
    var baseUrl = $"{this.HostAddress}/{handler}";

    var encoding = new UTF8Encoding();
    var bytes = encoding.GetBytes(data);

    var request = WebRequest.Create(baseUrl);

    if (options.AuthenticationType == AuthenticationType.Basic)
    {
        var encoded = Convert.ToBase64String(Encoding.GetEncoding("ISO-8859-1").GetBytes(options.UserName + ":" + options.Password));
        request.Headers[HttpRequestHeader.Authorization] = "Basic " + encoded;
    }

    request.Method = requestMethod;
    request.ContentType = "application/json";
#if NET451
    request.ContentLength = bytes.Length;
#endif

#if NETCORE
    var taskStream = request.GetRequestStreamAsync();
    taskStream.Wait();
    var stream = taskStream.Result;
    stream.Write(bytes, 0, bytes.Length);
#else
    var stream = request.GetRequestStream();
    stream.Write(bytes, 0, bytes.Length);
    stream.Close();
#endif

    return request;
}

The method calling this method is the following for GET:

public string Get(SecurityOptions options, string handler, string data)
{
    var request = this.Prepare(options, "GET-X", handler, data);

#if NETCORE
    var task = this.ExecuteAsync(request, data);
    task.Wait();

    return task.Result;
#else
    return this.Execute(request, data);
#endif
}

This Get method caused an error by using a request method GET-X which Solr itself (6.4.1) did not understand. I've changed this to a normal request method: GET therefore solving the error on solr's side. However currently i'm getting a System.Net.ProtocolViolationException with the message: Cannot send a content-body with this verb-type. This is happening when waiting for the taskStream to finish and write its result to the request-body.

My question:

How would one send a GET-request with a body (in json format (as string)) within dotnet core?

Since RFC2616 says it's not forbidden i'd like to use this 'feature' as answered in the following question

See RFC2616 - Hypertext Transfer Protocol -- HTTP/1.1, section 4.3 "Message Body":

A message-body MUST NOT be included in a request if the specification of the > > request method (section 5.1.1) does not allow sending an entity-body in requests. In section 9.3 "GET" including an entity-body is not forbidden.

So, yes, you are allowed to send an entity-body with a HTTP GET request.

Community
  • 1
  • 1
Baklap4
  • 3,914
  • 2
  • 29
  • 56
  • It's very unnatural to `GET` something with body. Get is for getting the resource with a query string, not about posting some data to server. – VMAtm Mar 11 '17 at 03:50
  • @VMAtm i know, but since it's not forbidden it can be used some usages are elastic search as suggested here: http://stackoverflow.com/a/29656364/2724940 So i wonder if it is it limit for dotnet core and not possible at all or if there's some kind of workaround. Since solr-express creates a messagebody for get requests – Baklap4 Mar 13 '17 at 08:49

0 Answers0