HTTP-клиент Apache, эквивалентный команде CURL для настройки шейп-файлов - PullRequest
1 голос
/ 26 марта 2019

Что эквивалентно коду httpclient для следующей команды CURL

 curl -v -u username:password-XPUT -H "Content-type: text/plain" -d "E:/path_to_shapefile/shapefiles/" "http://172.16.17.86:9090/geoserver/rest/workspaces/IDIRA6/datastores/scenario2373/external.shp?configure=all"

Команда CURL работает нормально. У меня ограниченные знания httpclient, однако, при адаптации подобного кода моя попытка заключается в следующем:

    import org.apache.http.client.fluent.*;

    public class QuickStart {
        public static void main(String[] args) throws Exception {  
            Executor executor = Executor.newInstance()
                    .auth("username", "password")
                    .authPreemptive("172.16.17.86:9090");
            // Line below does not compile
            String response = executor.execute(Request.Put("E:/path_to_shapefile/shapefiles/" 
     "http://172.16.17.86:9090/geoserver/rest/workspaces/IDIRA6/datastores/scenario2373/external.shp?configure=all"))                                
                     .returnResponse()
                     .toString();
             System.out.println(response);
        }
    }

Этот код выше не компилируется, так как я не знаю, как кодировать два URL в одном запросе, как в команде CURL. Будет приветствоваться исправление вышеуказанного кода или новый подход.

Заранее спасибо.

Ответы [ 2 ]

0 голосов
/ 26 марта 2019

Спасибо за ответ.Я заменил bodyFile на bodyString, и это сработало.

import org.apache.http.client.fluent.*;
import org.apache.http.entity.ContentType;

public class QuickStart {
    public static void main(String[] args) throws Exception {  
        Executor executor = Executor.newInstance()
                .auth("admin", "geoserver")
                .authPreemptive("172.16.17.86:9090");       
        String response = executor.execute(Request.Put("http://172.16.17.86:9090/geoserver/rest/workspaces/IDIRA6/datastores/scenario2373/external.shp?configure=all")
                .bodyString("E:\\Tomcat\\apache-tomcat-8.5.37\\webapps\\geoserver\\data\\data\\IDIRA6\\scenario2373\\", ContentType.create("text/plain")))
                 .returnResponse()
                 .toString();
         System.out.println(response);
    }
}
0 голосов
/ 26 марта 2019
import org.apache.http.client.fluent.*;
import org.apache.http.entity.ContentType;

public class QuickStart {
    public static void main(String[] args) throws Exception {  
        Executor executor = Executor.newInstance()
                .auth("admin", "geoserver")
                .authPreemptive("172.16.17.86:9090");       
        String response = executor.execute(Request.Put("http://172.16.17.86:9090/geoserver/rest/workspaces/IDIRA6/datastores/scenario2373/external.shp?configure=all")
                .bodyString("E:\\Tomcat\\apache-tomcat-8.5.37\\webapps\\geoserver\\data\\data\\IDIRA6\\scenario2373\\", ContentType.create("text/plain")))
                 .returnResponse()
                 .toString();
         System.out.println(response);
    }
}
...