发送查询与获取请求?

问题描述

我想发送带有get请求的查询(正文),我知道这没有意义,但我想像在post请求中一样在另一个端点(这里是Fuseki服务器)执行get查询

public static final MediaType sparqlGet = MediaType.get("application/x-www-form-urlencoded; charset=utf-8");

static OkHttpClient client = new OkHttpClient();
    
public static String test() throws IOException,InterruptedException {
         RequestBody body = RequestBody.create(
                 "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-Syntax-ns#>\r\nPREFIX rdf: <http://www.w3.org/1999/02/22-rdf-Syntax-ns#>\r\nPREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\r\nSELECT * WHERE {\r\n  ?sub <http://www.semanticweb.org/hightech/ontologies/2019/8/untitled-ontology-4#domainName> ?obj .\r\n}\r\n",sparqlGet);
            Request request = new Request.Builder()
                .url("http://localhost:3030/eduDataSet/query")
                .get()
                .build();
            try (Response response = client.newCall(request).execute()) {
              return response.body().string();
            }catch (Exception e) {
                return "exc";
            }


    public static String posttest() throws IOException {
    
        RequestBody body = RequestBody.create(
                "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-Syntax-ns#>\r\nPREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\r\nPREFIX xx: <http://www.semanticweb.org/hightech/ontologies/2019/8/untitled-ontology-4#>\r\nINSERT DATA{\r\n\t<http://www.semanticweb.org/hightech/ontologies/2019/8/untitled-ontology-4#Domain5> xx:domainName \"spring\".\r\n  \t<http://www.semanticweb.org/hightech/ontologies/2019/8/untitled-ontology-4#Domain5> xx:domainId \"spring\".\r\n\r\n}\r\n\r\n",sparql);

        Request request = new Request.Builder()
            .url ("http://localhost:3030/eduDataSet/update")
            .post(body)
            .build();
        try (Response response = client.newCall(request).execute()) {
          return response.body().string();
        }catch (Exception e) {
            return "exc";
        }
    }

解决方法

GET没有请求正文。因此,所有参数都必须出现在URL或标题中。

,

尝试这样做不是一个好主意,即使服务器会忽略它,在GET中发送参数的唯一方法是使用URL。

在这里查看说明:https://dropbox.tech/developers/limitations-of-the-get-method-in-http