I have written a J2ME application using WTK 2.5.2 with JDK 1.8.0_361 and JDK
1.5.0_22.
The following code snippet is used to connect to an HTTP server and it works as expected.
try{
http = (HttpConnection)Connector.open(url);
http.setRequestMethod(HttpConnection.POST);
OutputStream os = http.openOutputStream();
String params = "XXXXXXXX";
os.write(params.getBytes());
respCode = http.getResponseCode();
}catch(Exception e){
System.out.println(e);
}
However, when trying to connect to an HTTPS site, it throws one of these two errors:
Google:
java.io.IOException: Bad record type (21) or version (3.1)
Wikipedia:
java.io.IOException: Alert (2,40)
I have tried using HttpsConnection and switching to another WTK and JRE version.
Exporting the application to device shows another error:
java.io.IOException: Alert (2, 70)
Thanks in advance.
Related
I'm having issues retrieving the content from some HTTPS sites using the Java 11 HTTPClient. Here's the code I'm using:
HttpClient client = HttpClient.newBuilder().build();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://en.wikipedia.org/wiki/Main_Page"))
.build();
HttpResponse<byte[]> response = client.send(request, HttpResponse.BodyHandlers.ofByteArray());
System.out.println(response.statusCode());
System.out.println(new String(response.body()));
When trying to download the source of Wikipedia, I get the following error:
java.io.IOException: Received fatal alert: handshake_failure
If I use the same code but change the source URL to https://www.google.com, it works fine. From a bit of Googling, this makes it sound to me like it's probably an issue with differing crypto algorithms (although I don't know that), but it's not clear to me how to fix it. I've tried a few solutions that have been suggested on other StackOverflow questions, but haven't found one that works for me or a reliable guide to debugging the problem.
Can someone please point me in the direction of what I can try to fix the issue?
$ java -version
openjdk version "11.0.11" 2021-04-20
OpenJDK Runtime Environment (build 11.0.11+9-Ubuntu-0ubuntu2.20.04)
OpenJDK 64-Bit Server VM (build 11.0.11+9-Ubuntu-0ubuntu2.20.04, mixed mode, sharing)
I don't know what caused problem on your environment, but just to test the URL I ran a different Http client using Java 8, and it worked just fine. Just in case you would want to try this Http client Here is the code and the info where to get the client:
private static void testHttpClient() {
HttpClient client = new HttpClient();
client.setConnectionUrl("https://en.wikipedia.org/wiki/Main_Page");
String result;
try {
ByteBuffer buff = client.sendHttpRequestForBinaryResponse(HttpMethod.GET);
result = new String(buff.array());
// result = client.sendHttpRequest(HttpMethod.GET);
System.out.println(result);
} catch (IOException e) {
result = TextUtils.getStacktrace(e, "com.mot.");
}
}
This code worked with both options: you can comment the two lines
ByteBuffer buff = client.sendHttpRequestForBinaryResponse(HttpMethod.GET);
result = new String(buff.array());
and uncomment the line
// result = client.sendHttpRequest(HttpMethod.GET);
And it also works.
The HttpClient used here comes from MgntUtils Open Source library. That library is available as Maven Artifacts Maven Central here And also on GitHub (including source code and Javadoc). Javadoc for The library could be seen here.
Disclaimer: The library is written by me
I have a problem in my code. I need to push code to Heroku (running on Ubuntu 14.04); my code is PHP and Oracle database. oci_connect() works perfectly in localhost and connect successfully to the Oracle database but when I push to Heroku it's returning "Call to undefined function oci_connect()". I tried to install bitbucket-heroku like Ruby and Java, but nothing has worked for me yet.
Image: Heroku error app
This is my link in Heroku: https://serene-anchorage-79470.herokuapp.com/home.php?language=en
My config code:
$conn = oci_connect('username', 'pass','(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST =myhost )(PORT =port )) (CONNECT_DATA = (SID =sid )))');
if($conn)
{
echo "Successfully connected to Oracle.\n";
}
else
{
$err = OCIError();
echo "Connection failed." . $err[text];
}
The reason you are getting this error is because you are missing the OCI8 PHP extension which is needed in order to connect to Oracle database using PHP.
If your Oracle database is not hosted on the same server, you will also need to install Oracle Instant Client on the linux server.
I am trying to make a HTTP POST request to a server with https. When I was testing making the call I did it on a standalone java program (not deployed on a server) and initaially got the error:
unable to find valid certification path to requested target
however I fixed this with creating a cacert using a tool online called InstallCert and pointing java there with:
System.setProperty("javax.net.ssl.trustStore", "C:\\Users\\...\\jssecacerts");
However, when I implement the same fix in the code that I deploy on Apache Tomcat it doesnt work and I get the same error as before. So my question is do .war files deployed on Apache Tomcat use a different trust store than the java one? Or do they not use the system properties? How can I get Apache Tomcat to use the cacert?
Thanks
EDIT:
So I've tried to do some more research and get it fixed with no success. I tried following http://andyarismendi.blogspot.co.uk/2012/01/changing-tomcats-ca-trust-keystore-file.html to try and get it fixed.
Changing the server.xml file made no difference. However, changing the Tomcat Java Options with
-Djavax.next.ssl.trustStore="myFile"
Altered the response that I got, with this I get:
java.io.IOException: Server returned response code 500 for URL websitesURL
I also saw in some places that copying the jssecacerts file into my jre\lib\security directory could fix this. However this just cause the response code 500 error...
This is the code that is being used to make the POST Request:
//System.setProperty("javax.net.ssl.trustStore", "C:\\file\\path\\jssecacerts");
try
{
OAuthClient client = new OAuthClient(new URLConnectionClient());
//grant_type=password
OAuthClientRequest.TokenRequestBuilder trb = new OAuthClientRequest.TokenRequestBuilder(TOKEN_URL)
.setGrantType(GrantType.PASSWORD)
.setClientId(CLIENT_ID)
.setUsername(username)
.setPassword(password);
OAuthClientRequest ocr = trb.buildBodyMessage();
ocr.setHeader("Authorization", base64EncodedBasicAuthentication(CLIENT_ID, CLIENT_SECRET));
String responseBody = client.accessToken(ocr, OAuthJSONAccessTokenResponse.class).getBody();
return responseBody;
}
catch(Exception e)
{
e.printStackTrace();
return e.getMessage();
}
I'm trying to scan the Files for viruses using Clam Antivirus which is installed on Ubuntu on different machine from java code which is again on different machine. Below is my java code lines
//connect to Linux server
ClamScan clamScan = new ClamScan("127.0.0.1", 3310, 20);
// Scanning the test file
ScanResult result = clamScan.scan(new FileInputStream("D:\\vcredist_x64.png"));
I have done the necessary changes in Clamd.conf files that is to add the Tcp Port and Address , but still getting below error description
ERROR com.philvarner.clamavj.ClamScan - could not connect to clamd
server java.net.ConnectException: Connection refused: connect
So any body can help me to find out what is going wrong either in code or configuration .
Thanks in Advance.
Ali
Finally found the solution , actually clam daemon was not up and running .
I downloaded Java Card Connected Edition 3.0.2 from Oracle official website and installed it. There are some web samples in JCDK. In documentation written:
All samples must be run from within the NetBeans IDE. They cannot be run from
the command line in this release of the development kit.
Samples works with NetBeans IDE correctly. I can import them and run on Java Card Platform.
But I want to use this samples on Eclipse IDE. In Eclipse as Java Card SDK path I showed Java Card Connected home path. Then created new device and tried to start it [CardHolderApp for example]. But Eclipse gives:
ApduTool thread exited
User input thread exited
APDU|ApduTool [v3.0.2]
APDU| Copyright (c) 2009 Sun Microsystems, Inc.
APDU| All rights reserved.
APDU| Use is subject to license terms.
APDU|Opening connection to localhost on port 9025.
APDU|Connected.
APDU|java.net.ConnectException: Connection refused: connect
ApduTool process finished with code: 1
.log file:
!ENTRY org.eclipse.core.jobs 4 2 2015-09-08 01:39:17.142
!MESSAGE An internal error occurred during: "Launching CardHolderApp".
!STACK 0
java.lang.RuntimeException: Cannot start device. Please see the log.
at com.oracle.javacard.jcdk.launch.runconfiguration.AppletRunConfigurationDelegate.launch(AppletRunConfigurationDelegate.java:79)
at org.eclipse.debug.internal.core.LaunchConfiguration.launch(LaunchConfiguration.java:885)
at org.eclipse.debug.internal.core.LaunchConfiguration.launch(LaunchConfiguration.java:739)
at org.eclipse.debug.internal.ui.DebugUIPlugin.buildAndLaunch(DebugUIPlugin.java:1039)
at org.eclipse.debug.internal.ui.DebugUIPlugin$8.run(DebugUIPlugin.java:1256)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:55)
Is it possible to run Java Card Connected Web project in Eclipse IDE?
UPDATE:
Device started successfully. But project not deployed. It gives "unsupported String type constant" on every String usage.
Code example:
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
response.setContentType("text/html"); // unsupported String type constant
PrintWriter out = response.getWriter();
RequestDispatcher dispatcher = null;
dispatcher = request.getRequestDispatcher("/WEB-INF/header.i");//unsupported String type constant
response.sendRedirect(response.encodeRedirectURL("http://www.sun.com"));//unsupported String type constant
dispatcher.include(request, response);
dispatcher.include(request, response);
}
Classic Edition did not support Strings. But it must work on Connected Edition.
Forgetting about all the complicated software in between. Just looking at the error messages, it looks like port 9025 is not open or available on your localhost :
APDU|Opening connection to localhost on port 9025.
APDU|Connected.
APDU|java.net.ConnectException: Connection refused: connect
ApduTool process finished with code: 1
The text [java.net.ConnectException: Connection refused: connect], everytime I see "Connection refused" it means java tried to create a socket on the port (9025 in this case) and it could not get the port number either because of a blockage (like a firewall) or not available (something else is using port 9025).
On windows or *nix, you can usually use the netstat command to see what ports are being used.
hth, adym