0

I know this question had several answers but none of them seem to work for my case.

The operation I'm trying to do in curl is curl -u 'user:pwd' https://localhost:8000/services/search/jobs --data search='search index=my_index'

For instance, I am able to do this in python with:

import urllib
import httplib2
from xml.dom import minidom

baseurl = 'https://localhost:8000'
userName = 'user'
password = 'pwd'
searchQuery = 'index=my_index'
serverContent = httplib2.Http(disable_ssl_certificate_validation=True).request(baseurl + '/services/auth/login',
                                                                               'POST', headers={}, body=urllib.parse.urlencode({'username':userName, 'password':password}))[1]
sessionKey = minidom.parseString(serverContent).getElementsByTagName('sessionKey')[0].childNodes[0].nodeValue
searchQuery = searchQuery.strip()
if not (searchQuery.startswith('search') or searchQuery.startswith("|")):
    searchQuery = 'search ' + searchQuery
print (httplib2.Http(disable_ssl_certificate_validation=True).request(baseurl + '/services/search/jobs','POST',
                                                                   headers={'Authorization': 'Splunk %s' % sessionKey},body=urllib.parse.urlencode({'search': searchQuery}))[1])

But every solution I looked at for java does not seem to work for me. Is there an equivalent of httplib2.Http(disable_ssl_certificate_validation=True).request(...) in Java?

This is what I've tried

String rawData = "search='search index=my_index'";
System.out.println(rawData);
String encodedData = URLEncoder.encode( rawData, "UTF-8" );
String name = "user";
String password = "pwd";
String authString = name + ":" + password;
System.out.println("auth string: " + authString);
byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
String authStringEnc = new String(authEncBytes);
URL u = new URL("https://localhost:8000/services/search/jobs");
HttpURLConnection conn = (HttpURLConnection) u.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Authorization", "Basic " + authStringEnc);
conn.setRequestProperty("Content-Type", "application/json; utf-8");
conn.setRequestProperty( "Content-Length", String.valueOf(encodedData.length()));
InputStream is = conn.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
int code = conn.getResponseCode();
System.out.println("Response    (Code):" + code);
System.out.println("Response (Message):" + conn.getResponseMessage());
DataInputStream input = new DataInputStream(conn.getInputStream());
int c;
StringBuilder resultBuf = new StringBuilder();
while ((c = input.read()) != -1) {
  resultBuf.append((char) c);
}
input.close();
System.out.print(resultBuf.toString());**/
Tom
  • 41
  • 3
  • What did you try? What happened? – SLaks Sep 05 '19 at 18:20
  • https://www.mkyong.com/java/how-to-send-http-request-getpost-in-java/ and https://stackoverflow.com/questions/3324717/sending-http-post-request-in-java But both of these seemed to return the incorrect xml data. It's just supposed to return an SID like it does in python and in terminal with the curl command. – Tom Sep 05 '19 at 18:21
  • Are you sure you should be using `DataInputStream`? It is for a Java specific data serialization protocol. – Mark Rotteveel Sep 07 '19 at 07:44

1 Answers1

1

I have been able to use Jsoup for most of my http needs (mostly web scraping).

Here is some code to post an event I have pulled from javacodeexamples.com

https://www.javacodeexamples.com/jsoup-post-form-data-example/822

Response response = 
                Jsoup.connect("http://www.example.com/postpage")
                .userAgent("Mozilla/5.0")
                .timeout(10 * 1000)
                .method(Method.POST)
                .data("txtloginid", "YOUR_LOGINID")
                .data("txtloginpassword", "YOUR_PASSWORD")
                .data("random", "123342343")
                .data("task", "login")
                .data("destination", "/welcome")
                .followRedirects(true)
                .execute();
Danny Piper
  • 43
  • 1
  • 1
  • 5