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