-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
70 lines (59 loc) · 2.59 KB
/
Copy pathMain.java
File metadata and controls
70 lines (59 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package com.testingbot;
import org.apache.http.*;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.AuthCache;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.client.utils.URIUtils;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.BasicAuthCache;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
public class Main {
public static void main(String[] args) {
final String testingBotKey = System.getenv("TESTINGBOT_KEY");
final String testingBotSecret = System.getenv("TESTINGBOT_SECRET");
String urlApi = "https://api.testingbot.com/v1/storage";
HttpClient httpClient = HttpClientBuilder.create()
.build();
URI url = null;
try {
url = new URI(urlApi);
} catch (URISyntaxException e) {
e.printStackTrace();
}
HttpPost post = new HttpPost(url);
HttpEntity entity = MultipartEntityBuilder.create().addPart("file", new FileBody(new File(args[0]))).build();
post.setEntity(entity);
HttpResponse response;
try {
HttpHost httpHost = URIUtils.extractHost(new URI(urlApi));
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials(testingBotKey, testingBotSecret));
AuthCache authCache = new BasicAuthCache();
authCache.put(httpHost, new BasicScheme());
HttpClientContext context = HttpClientContext.create();
context.setCredentialsProvider(credsProvider);
context.setAuthCache(authCache);
response = httpClient.execute(post, context);
HttpEntity responseEntity = response.getEntity();
String responseString = EntityUtils.toString(responseEntity,"UTF-8");
System.out.println(responseString);
} catch (IOException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
}