I'm using Java to check the response of a url I pass in. This is the current code I'm using. However, I keep getting a connection refused error. I'm testing with the basic Google home page so that I know the rough draft of my code works.
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
public class connectTest {
public static void main(String[] args)
{
boolean test = pageExists();
}
public static boolean pageExists(){
int returnCode= 0;
try {
HttpURLConnection.setFollowRedirects(false);
String httpsURL = "https://www.google.com/";
URL testUrl = new URL(httpsURL);
HttpsURLConnection con =
(HttpsURLConnection)testUrl.openConnection();
returnCode= con.getResponseCode();
System.out.println(returnCode);
} catch (Exception e) {
e.printStackTrace();
}
if (returnCode== HttpURLConnection.HTTP_OK){
return true;
}
else if (returnCode== HttpURLConnection.HTTP_BAD_REQUEST || returnCode==
HttpURLConnection.HTTP_NOT_FOUND ){
return false;
}
else
{
System.out.println("The return code was not 200,400 or 404");
return false;
}
}
}
This is the error I get when I run the following:
java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.security.ssl.SSLSocketImpl.connect(Unknown Source)
at sun.security.ssl.BaseSSLSocketImpl.connect(Unknown Source)
at sun.net.NetworkClient.doConnect(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.protocol.https.HttpsClient.<init>(Unknown Source)
at sun.net.www.protocol.https.HttpsClient.New(Unknown Source)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at java.net.HttpURLConnection.getResponseCode(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(Unknown Source)
I am learning java. I've tried to access a website and see how many characters are there. A website I tried is:
http://cs.armstrong.edu/liang/data/Lincoln.txt
I get a I/O error: no such file output. but I shouldn't get since the web site is up & running.
package ikinciHaftam;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Scanner;
public class ReadFileFromURL {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.print("Enter a URL: ");
Scanner inURL = new Scanner(System.in);
String URLString = inURL.next();
inURL.close();
try{
URL url = new URL(URLString);
int count =0;
Scanner input = new Scanner(url.openStream());
System.out.println(url.openStream());
while (input.hasNext()){
String line = input.nextLine();
count += line.length();
}
System.out.println("The file size is " + count + " characters");
input.close();
}
catch(MalformedURLException ex){
System.out.println("Malformed url");
}
catch(IOException e){
e.printStackTrace();
}
}
}
and I get:
java.net.UnknownHostException: cs.armstrong.edu
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.net.NetworkClient.doConnect(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.<init>(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at java.net.URL.openStream(Unknown Source)
at ikinciHaftam.ReadFileFromURL.main(ReadFileFromURL.java:18)
i tried to get view source(html page) from websit
i used whit this code
public static void main(String[] args) {
URL url;
InputStream is = null;
BufferedReader br;
String line;
try {
url = new URL("http://stackoverflow.com/");
is = url.openStream(); // throws an IOException
br = new BufferedReader(new InputStreamReader(is));
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (MalformedURLException mue) {
mue.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
if (is != null) is.close();
} catch (IOException ioe) {
// nothing to see here
}
}
}
and when i bring to line "is = url.openStream();
i received exception "java.net.UnknownHostException"
java.net.UnknownHostException: stackoverflow.com
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.net.NetworkClient.doConnect(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.<init>(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at java.net.URL.openStream(Unknown Source)
at test.main.main(main.java:17)
do you know how i can fix this code
thanks a lot,
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.PrintWriter;
import java.sql.*;
import java.net.*;
public class connection {
JTextField textfeild;
JButton button;
String text;
Socket sock;
PrintWriter writer;
JButton button1;
public static void main(String[] args) {
connection user1 = new connection();
user1.go();
}//main method close
public void go() {
JFrame frame12 = new JFrame();
JPanel centerpanel12 = new JPanel();
centerpanel12.setLayout(new BoxLayout(centerpanel12, BoxLayout.Y_AXIS));
textfeild = new JTextField(20);
centerpanel12.add(textfeild);
//textfeild.addActionListener(new textfeildlitner());
frame12.add(centerpanel12);
button = new JButton("Click Me");
centerpanel12.add(button);
button.addActionListener(new buttonlitner());
button1 = new JButton("DataDisplay");
centerpanel12.add(button1);
button1.addActionListener(new buttonlitner1());
frame12.getContentPane().add(BorderLayout.CENTER, centerpanel12);
frame12.pack();
frame12.setVisible(true);
frame12.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}//go method close
/*class textfeildlitner implements ActionListener{
public void actionPerformed(ActionEvent ev){
}
}//inner class textfeildlitner close*/
class buttonlitner implements ActionListener {
public void actionPerformed(ActionEvent ev) {
button.setText("I AM Clicked");
String name = textfeild.getText();
System.out.println(name);
textfeild.setText("");
textfeild.requestFocus();
}//method close
}//inner class close
class buttonlitner1 implements ActionListener {
void connection() {
try {
String user = "SQlUI";
String pass = "123456";
String db = "jdbc:sqlserver://localhost:1234;" + ";databaseName=SQlUI";
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con = DriverManager.getConnection(db, user, pass);
Statement s1 = con.createStatement();
ResultSet r1 = s1.executeQuery("select * from Table_1");
String[] result = new String[20];
if (r1 != null) {
while (r1.next()) {
for (int i = 0; i < result.length; i++) {
for (int j = 0; j < result.length; j++) {
result[j] = r1.getString(i);
System.out.println(result[j]);
break;
}//for j close
}//for i close
}//if closeclose
}//try close
} catch (Exception ex) {
ex.printStackTrace();
}//catch close
}//connection() close
public void actionPerformed(ActionEvent ev) {
button1.setText("Processing");
new buttonlitner1().connection();
}//method close
}//inner class close
}//outer class close
And the exception which i am getting for this code-
com.microsoft.sqlserver.jdbc.SQLServerException: The index 0 is out of range.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:190)
at com.microsoft.sqlserver.jdbc.SQLServerResultSet.verifyValidColumnIndex(SQLServerResultSet.java:531)
at com.microsoft.sqlserver.jdbc.SQLServerResultSet.getterGetColumn(SQLServerResultSet.java:2049)
at com.microsoft.sqlserver.jdbc.SQLServerResultSet.getValue(SQLServerResultSet.java:2082)
at com.microsoft.sqlserver.jdbc.SQLServerResultSet.getValue(SQLServerResultSet.java:2067)
at com.microsoft.sqlserver.jdbc.SQLServerResultSet.getString(SQLServerResultSet.java:2392)
at connection$buttonlitner1.connection(connection.java:76)
at connection$buttonlitner1.actionPerformed(connection.java:89)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
When i am changing the value of i from 0 to 1 then it displays result for 1st row but again it throws exception saying that the index 3 is out of range.
I have posted the same code and after making the changes (as suggested by the experts on stackoverflow) it gave me a new type of exception.
I am putting the link of the previous question-Getting Checked Exception while running a SQL Program and the question was Getting Checked Exception while running a SQL Program
In SQL (unlike generally in Java) evrything is indexed starting from 1 - both rows and columns.
Why do you have 2 loops inside your r1.next() loop?
Think what your code
for (int j = 0; j < result.length; j++) {
result[j] = r1.getString(i);
System.out.println(result[j]);
break;
}
actually does.
I am working on a program in Java that requires the user to log in, however when is end the HTTP request and try to read the output it returns HTTP response code 500.
The code in Login.java is as follows:
package net.discfiresoftworks.chat;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
public class Login extends JFrame
{
private static final long serialVersionUID = 1L;
public JTextField user = new JTextField();
public JPasswordField pass = new JPasswordField(20);
public JButton submit = new JButton("Log In");
public JButton create = new JButton("Sign-Up");
public JPanel main = new JPanel();
public JPanel buttons = new JPanel();
public Login()
{
setTitle("DiscFire Account Login");
setSize(350, 180);
setDefaultCloseOperation(3);
setLayout(new FlowLayout(1));
setLocationRelativeTo(null);
setResizable(false);
getContentPane().setBackground(new Color(50, 50, 50));
user.setColumns(20);
pass.setColumns(20);
pass.setBorder(BorderFactory.createCompoundBorder(new EmptyBorder(10, 0, 10, 0), pass.getBorder()));
submit.setBorder(BorderFactory.createLineBorder(Color.GRAY, 1));
create.setBorder(BorderFactory.createLineBorder(Color.GRAY, 1));
submit.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e)
{
String password = new String(pass.getPassword());
sendRequest(user.getText(), password);
}
});
create.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e)
{
}
});
buttons.setLayout(new FlowLayout());
buttons.add(submit);
buttons.add(create);
main.setLayout(new BoxLayout(main, BoxLayout.Y_AXIS));
main.setBorder(BorderFactory.createLineBorder(Color.WHITE, 10));
main.add(user);
main.add(pass);
main.add(buttons);
add(main);
}
public void open()
{
setVisible(true);
}
public void sendRequest(String user, String pass)
{
String output = "";
try
{
String urlParameters = "user=" + URLEncoder.encode(user,"UTF-8") + "&pass=" + URLEncoder.encode(pass,"UTF-8");
URL url = new URL("http://www.discfiresoftworks.net/plogin.php");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setInstanceFollowRedirects(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("charset", "utf-8");
connection.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.getBytes().length));
connection.setUseCaches(false);
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
System.out.println(urlParameters);
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
output = in.readLine();
in.close();
connection.disconnect();
}catch(Exception e){
e.printStackTrace();
}
String sid = (output.split(":"))[0];
String admin = (output.split(":"))[1];
System.out.println(sid = " " + admin);
}
}
The code in plogin.php is as follows:
<?php
include 'includes/conn.php';
include 'includes/hash.php'
session_start();
if(isset($_POST['user']) && isset($_POST['pass'])){
$user = $_POST['user'];
$password = $_POST['pass'];
if(isset($_POST['redir'])){
$redir = $_POST['redir'];
}else{
$redir = '';
}
$query = $conn->prepare("SELECT Password, Verified, Admin FROM Users WHERE Name=?");
$query->bind_param('s', $user);
if($query->execute()){
$query->bind_result($hash, $ver, $admin);
while($query->fetch()){
if($ver == "T"){
if($query->num_rows > 0){
if(validate_password($password, $hash)){
$_SESSION['user'] = $user;
$msg = session_id() . ':' . $user;
}else{
$msg = 'Incorrect username or password';
}
}else{
$msg = 'Incorrect username or password';
}
}else{
$msg = 'Your account has not been verified, please check your email and do so now if you wish to continue.';
}
}
}else{
$msg = $query->errno . " " . $query->error;
}
}else{
$msg = 'Missing username or password';
}
echo $msg;
?>
Thanks in advance, I hope you can help me :)
Edit: I know it is not a fault in conn.php or hash.php because I have used both of them before and they work fine.
Edit: Stacktrace:
java.io.IOException: Server returned HTTP response code: 500 for URL: http://www.discfiresoftworks.net/plogin.php
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at net.discfiresoftworks.chat.Login.sendRequest(Login.java:116)
at net.discfiresoftworks.chat.Login$1.actionPerformed(Login.java:58)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1
at net.discfiresoftworks.chat.Login.sendRequest(Login.java:128)
at net.discfiresoftworks.chat.Login$1.actionPerformed(Login.java:58)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
I had the same problem and solved now.
Just check for correct encoding. make sure you have encoded everything to the same charset.
in my case I am using ISO8859_1 encoding. the server was responding OK to firefox browser but error500 to my java program. I changed to ISO8859_1 and now it is working:
public void sendPost(String Url, String params) throws Exception {
String url=Url;
URL obj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
con.setRequestProperty("Acceptcharset", "en-us");
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
con.setRequestProperty("charset", "EN-US");
con.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
String urlParameters=params;
// Send post request
con.setDoOutput(true);
con.setDoInput(true);
con.connect();
//con.
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + urlParameters);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
//print result
System.out.println(response.toString());
this.response=response.toString();
con.disconnect();
}
In the main program I call it like this:
certcon.sendPost("https://www.desphilboy.com/authbox/generateCRT","csr_string="+URLEncoder.encode(requestparams,"ISO8859_1"));