/// <summary> /// Internal Helper Method which executes the HTTP Requests against the Sparql Endpoint. /// </summary> /// <param name="target">Uri to make Request to.</param> /// <param name="postData">Data that is to be POSTed to the Endpoint in <strong>application/x-www-form-urlencoded</strong> format.</param> /// <param name="accept">The Accept Header that should be used.</param> /// <returns>HTTP Response.</returns> private HttpWebResponse ExecuteQuery(Uri target, String postData, String accept) { // Expect errors in this function to be handled by the calling function // Set-up the Request HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(target); // Use HTTP GET/POST according to user set preference httpRequest.Accept = accept; if (!postData.Equals(string.Empty)) { httpRequest.Method = "POST"; httpRequest.ContentType = MimeTypesHelper.Utf8WWWFormURLEncoded; using (var writer = new StreamWriter(httpRequest.GetRequestStream(), new UTF8Encoding(Options.UseBomForUtf8))) { writer.Write(postData); writer.Close(); } } else { if (HttpMode.Equals("AUTO")) { httpRequest.Method = postData.Equals(string.Empty) ? "GET" : "POST"; } else { httpRequest.Method = HttpMode; } } ApplyRequestOptions(httpRequest); Tools.HttpDebugRequest(httpRequest); HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse(); Tools.HttpDebugResponse(httpResponse); return(httpResponse); }
/// <summary> /// Internal method which builds the Query Uri and executes it via GET/POST as appropriate. /// </summary> /// <param name="sparqlQuery">Sparql Query.</param> /// <param name="acceptHeader">Accept Header to use for the request.</param> /// <returns></returns> private HttpWebResponse QueryInternal(String sparqlQuery, String acceptHeader) { // Patched by Alexander Zapirov to handle situations where the SPARQL Query is very long // i.e. would exceed the length limit of the Uri class // Build the Query Uri StringBuilder queryUri = new StringBuilder(); queryUri.Append(Uri.AbsoluteUri); bool longQuery = true; if (HttpMode.Equals("GET") || HttpMode.Equals("AUTO") && sparqlQuery.Length <= LongQueryLength && sparqlQuery.IsAscii()) { longQuery = false; try { queryUri.Append(!Uri.Query.Equals(string.Empty) ? "&query=" : "?query="); queryUri.Append(HttpUtility.UrlEncode(sparqlQuery)); // Add the Default Graph URIs foreach (string defaultGraph in _defaultGraphUris) { if (defaultGraph.Equals(string.Empty)) { continue; } queryUri.Append("&default-graph-uri="); queryUri.Append(HttpUtility.UrlEncode(defaultGraph)); } // Add the Named Graph URIs foreach (string namedGraph in _namedGraphUris) { if (namedGraph.Equals(string.Empty)) { continue; } queryUri.Append("&named-graph-uri="); queryUri.Append(HttpUtility.UrlEncode(namedGraph)); } } catch (UriFormatException) { if (HttpMode.Equals("GET")) { throw; } longQuery = true; } } // Make the Query via HTTP HttpWebResponse httpResponse; if (HttpMode.Equals("AUTO") && (longQuery || queryUri.Length > 2048) || HttpMode == "POST") { // Long Uri/HTTP POST Mode so use POST StringBuilder postData = new StringBuilder(); postData.Append("query="); postData.Append(HttpUtility.UrlEncode(sparqlQuery)); // Add the Default Graph URI(s) foreach (String defaultGraph in _defaultGraphUris) { if (defaultGraph.Equals(String.Empty)) { continue; } postData.Append("&default-graph-uri="); postData.Append(HttpUtility.UrlEncode(defaultGraph)); } // Add the Named Graph URI(s) foreach (String namedGraph in _namedGraphUris) { if (namedGraph.Equals(String.Empty)) { continue; } postData.Append("&named-graph-uri="); postData.Append(HttpUtility.UrlEncode(namedGraph)); } httpResponse = ExecuteQuery(Uri, postData.ToString(), acceptHeader); } else { // Make the query normally via GET httpResponse = ExecuteQuery(UriFactory.Create(queryUri.ToString()), String.Empty, acceptHeader); } return(httpResponse); }