1

How do I tell dotNetRDF to request and accept data from a remote triplestore where the response is encoded using gzip?

Looking at the source code for the LoadGraph method of SparqlHttpProtocolConnector, it doesn't appear to me to have a mechanism for setting the Accept-Encoding header, nor am I seeing any logic that would process a Content-Encoding header.

I tried modifying LoadGraph to set Accept-Encoding, and the content then comes back with the right Content-Type and Content-Encoding, but the line of code that determines how to handle the response is

IRdfReader parser = MimeTypesHelper.GetParser(response.ContentType);

and GetParser doesn't have any logic that considers the Content-Encoding.

However, it seems like the pieces are present: there's certainly infrastructure in place to process a gzipped file.

Is there another way to do this that I'm missing, or would this be a new feature request?

Thanks.

4

1 回答 1

2

您可以扩展SPARQLHttpProtocolConnector然后覆盖该ApplyCustomRequestOptions方法以应用 Accept-Encoding 标头。

尽管MimeTypesHelper不区分响应的 Content-Encoding 标头,但您可以改为使用HttpWebRequest.AutomaticDecompression 属性来启用响应流的自动解压缩。同样,这可以在ApplyCustomRequestOptions方法中设置。

所以你的扩展类会是这样的:

public class CompressedSparqlHttpProtocolConnector : SparqlHttpProtocolConnector
{
  // Define appropriate constructors with the parameters you need e.g.
  public CompressedSparqlHttpProtocolConnector(Uri serviceUri)
: base(serviceUri) { }     

  protected override ApplyCustomRequestOptions(HttpWebRequest request)
  {
     // Request GZip encoded response, allow fallback to identity encoding
     request.Headers[HttpRequestHeader.ContentEncoding] = "gzip;q=1.0, identity;q=0.5"

     // Enable automatic decompression of the response
     request.AutomaticDecompression = DecompressionMethods.GZip;
  } 
}
于 2016-11-03T10:10:38.463 回答