Apache HTTP客户端等效于CURL命令来配置shapefile

问题描述 投票:1回答:2

以下CURL命令的httpclient代码等价物

 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);
        }
    }

上面的代码不能编译,因为我不知道如何在与CURL命令相同的请求中编码两个url。可以理解对上述代码或新方法的修复。

提前致谢。

java apache-httpclient-4.x
2个回答
0
投票
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
投票

谢谢你的回答。我用bodyString替换了bodyFile,这很有效。

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);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.