且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

击中Java Web服务:curl或URLConnection

更新时间:2022-06-27 05:54:41

我相信使用Java和 httpclient-4.x :

I believe this is more-or-less equivalent using Java and httpclient-4.x:

try {
    Credentials credentials = new UsernamePasswordCredentials("admin", "password");  //-u admin:password
    DefaultHttpClient httpClient = new DefaultHttpClient();
    httpClient.getCredentialsProvider().setCredentials(AuthScope.ANY, credentials);
    HttpPut put = new HttpPut("http://localhost:8080/my-server/file.ext");  //-X PUT
    put.setEntity(new FileEntity(new File("/absolute/path/file/to/upload/file.ext"), "image/png"));  //@ - absolute path
    httpClient.execute(put);
} catch(Exception e) {
    //-f, fail silently
}