POST request using client SSL Certificate

I am trying to connect to PG&E through their share my data program. They require the client to connect to their web service using a SSL Certificate.

I cannot figure out how to use system.net.httpPost with the keystore file I have.

Any help would be appreciated.

You’re going to have to do this using Java classes/library instead of system.net.httpPost()

This Stack Overflow post should get you going: stackoverflow.com/questions/3734 … entication

is Apache HTTP Client available in Ignition?

No, but you can add it with a module if you want. You’d be able to create your own HTTP scripting functions that way too.

The built-in scripting functions all use HttpURLConnection underneath.

out of curiosity what library is being used in the function system.net.httpPost?

[quote=“Kevin.Herron”]
The built-in scripting functions all use HttpURLConnection underneath.[/quote]

:smiley:

I figured how to do this with the libraries already available in java using HttpsURLConnection

[code]from java.net import URL
from java.security import KeyStore
from java.io import FileInputStream
from javax.net.ssl import SSLContext
from javax.net.ssl import SSLSocketFactory
from java.io import BufferedReader
from java.io import InputStreamReader
from java.io import OutputStreamWriter
from java.lang import String
from javax.net.ssl import KeyManagerFactory

ksCACert = KeyStore.getInstance(KeyStore.getDefaultType())
password = String(“Put Your Keystore Password HERE”)
ksCACert.load(FileInputStream(“Put your file path to your .jks Keystore file HERE”), password.toCharArray())

kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm())
kmf.init(ksCACert, password.toCharArray())

context = SSLContext.getInstance(“TLS”)
context.init(kmf.getKeyManagers(), None, None)
sslSocketFactory = context.getSocketFactory()

url = URL(“https://URL your are trying to access HERE”)
urlConnection = url.openConnection()
urlConnection.setDoOutput(1)
urlConnection.setDoInput(1)
urlConnection.setUseCaches(0)

urlConnection.setSSLSocketFactory(sslSocketFactory)
#THIS IS WHERE YOU ADD HEADER INFORMATION
#may be different headers for you application
urlConnection.setRequestProperty(“Authorization”,“Put access code here”)
urlConnection.setRequestProperty(“Content-Type”, “application/x-www-form-urlencoded”)
urlConnection.setRequestMethod(“POST”)

#this is where parameters go
#may not need this for your application
urlParameters = String(“param1=paraminput”)
os = urlConnection.getOutputStream()
osw = OutputStreamWriter(os)
osw.write(urlParameters)
osw.flush()
osw.close()
os.close()

urlConnection.connect()

responseCode = urlConnection.getResponseCode()
print "Sending ‘POST’ request to URL : " , url
print "Post parameters : " , urlParameters
print "Response Code : " , responseCode

isr = InputStreamReader(urlConnection.getInputStream())

br = BufferedReader(isr)

line = br.readLine()
while(line != None):
print line
line = br.readLine()

br.close()[/code]

1 Like