Add a proxy for URl - java

How do add proxy to access a url in java? For aceessing a url I have written a java program which will tell me wether the URl is accessible or not. But it is not working a giving me response code as 404 though the Url is accessible from the browser.
Please help.
This is my code
HttpURLConnection httpURLConnection;
StringBuffer strbufstatus = new StringBuffer();
try {
//Connecting to the url
targetURL = new URL(url);
start = System.currentTimeMillis();
httpURLConnection = (HttpURLConnection) targetURL.openConnection();
httpURLConnection.setUseCaches(false);
httpURLConnection.setAllowUserInteraction(false);
httpURLConnection.setDoInput(true);
httpURLConnection.setRequestMethod("GET");
httpURLConnection.connect();
//Getting the respond Code
int responseCode = httpURLConnection.getResponseCode();
strbufstatus.append("Response Code===> " + responseCode + "<br>");
if(responseCode==200){
// System.out.println("respondcode===> " + responseCode);
end = System.currentTimeMillis();
//Calculating the response time
difference = (end - start);
difference = difference / 1000;
// System.out.println("Response Time===> " + difference);
strbufstatus.append("Rsponse time===> " + difference + "<br>");
}
} catch (IOException ex) {
if (ex.toString().contains("java.net.UnknownHostException:")) {
strbufstatus.append(" - UnknownHostException has occured during Httpconnection\n");
} else if (ex.toString().contains("java.net.MalformedURLException: unknown protocol:")) {
strbufstatus.append(" - Unknown Protocol\n");
} else if (ex.toString().contains("java.net.ConnectException: Connection timed out: connect")) {
strbufstatus.append("Connection TimedOut\n");
} else {
strbufstatus.append("IOException has occured during Httpconnection \n");
}
ex.printStackTrace();
}
System.out.println("Status" +strbufstatus);
return strbufstatus.toString();
}

Look for specific java System.setProperty's for controlling proxy usage. Some pertinent key names are 'deployment.proxy.type' as well as others listed here.

Old but still working method is like below:
System.getProperties().put("http.proxySet", "true");
System.getProperties().put("http.proxyHost", "proxyHostname");
System.getProperties().put("http.proxyPort", "proxyPort");
System.getProperties().put("http.proxyUser", username);
System.getProperties().put("http.proxyPassword", password);

Related

HttpUrlConnection connection reset error when sending second message

Basically that's all, the first message sent is correctly received by the server, but all the next ones fail. This is the main loop that iterates over the messages and treates some errors for debugging and logic of the code. The variable udc is my database connection, but it works fine so ignore it.
for (MessageOutModel message : messageOutModels) {
HttpURLConnection connection = MakeConnection();
Date currentMoment = new Date(Calendar.getInstance().getTimeInMillis());
System.out.println("Sent message id: " + message.id);
OutputStreamWriter out = new OutputStreamWriter(
connection.getOutputStream());
out.write(message.contenido);
out.close();
//os.write(message.contenido);
System.out.println("Message sent");
int responseCode = connection.getResponseCode();
System.out.println("Connection message: " + responseCode);
message.f_ultimo_intento = currentMoment;
if (responseCode != 200) {
message.intentos++;
message.desc_error = "Connection error: " + responseCode
+ " Connection error message: " + connection.getResponseMessage() + "Response error:" +
connection.getResponseMessage();
udc.UpdateMessageOut(message);
if (message.intentos == configModel.n_reintentos) {
udc.InsertError("MAX_INTENTOS", "Se ha alcanzado el máximo número de " + "intentos para el id-ticket: " + message.id + "-" + message.ticket_number);
}
} else {
message.desc_error = "";
udc.UpdateMessageOut(message);
}
connection.disconnect();
}
There goes the method MakeConnection()
protected HttpURLConnection MakeConnection() throws IOException {
QName q = new QName(SERVER_URL, "");
URL url = new URL(q.getNamespaceURI() + q.getLocalPart());
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setInstanceFollowRedirects(false);
connection.setRequestProperty("UserName", Username);
connection.setRequestProperty("PassWord", Password);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", MediaType.TEXT_PLAIN);
return connection;
}
Connection reset error
Use this as a quick solution.
HttpURLConnection connection = MakeConnection();
for (MessageOutModel message : messageOutModels) {
// logic from your sample
}
connection.disconnect();
Also, you can use try-with-resources to improve design a little.
At the end it resulted to be a firewall problem with the client and the server. It was blocking the connection for every other time after the first one.

Posting data from Java and receiving in node.js applicaiton

I have written a demo in Java that posts data once a second:
public static void main(String[] arystrArgs) {
//Get Operating System name and version
String strOSname = System.getProperty("os.name").toLowerCase();
//Display application title and what it is running on
System.out.println("Java Data Posting Demo");
System.out.println("Build date: " + BUILD_DATE + ", Version: " + VERSION_NO);
System.out.println("Running on: " + strOSname.substring(0, 1).toUpperCase()
+ strOSname.substring(1).toLowerCase());
//Post data to server
HttpURLConnection conn = null;
while( true ) {
try {
Thread.sleep(DELAY_BETWEEN_POSTS);
URL url = new URL("http://localhost:8080");
conn = (HttpURLConnection)url.openConnection();
if ( conn != null ) {
//Whatever you wants to post...
String strPostData = "p1=Hello&p2=" + (new Date()).getTime();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-length", Integer.toString(strPostData.length()));
conn.setRequestProperty("Content-Language", "en-GB");
conn.setRequestProperty("charset", "utf-8");
conn.setUseCaches(false);
conn.setDoOutput(true);
DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(strPostData);
dos.close();
System.out.println("Post to: " + url.toString() + ", data: " + strPostData);
}
} catch (InterruptedException|IOException ex) {
//ex.printStackTrace();
} finally {
if ( conn != null ) {
conn.disconnect();
conn = null;
}
}
}
}
I have written a Node.js application that listens on port 8080, but I don't see any POST requests in the http handler, I only see GET requests when I use a browser on the same address and port to test.
Snippet from node.js application:
function defaultHandler(request, response) {
try{
if ( request.method == "POST" ) {
var strBody = "";
request.on("data", function(chunk) {
strBody += chunk;
});
request.on("end", function() {
console.log("Received posted data: " + strBody);
});
} else {
console.dir(request);
}
} catch( ex ) {
console.dir(ex);
}
};
var app = http.createServer(defaultHandler);
app.listen(8080);
This is a cut down version, but all I ever see is get requests. I can see that the Java is connecting and posting data, as when I start Node.js and only when I start Node.js, the Java application connects to the URL then starts POSTING with a second delay between posts, it I terminate node, then it stops posting and restarting node causes the posts to resume.
Your Node app never send response to client. You send a lot of request but Java Client never receive response from server. You should execute end method on response.
var http = require('http');
function defaultHandler(request, response) {
try {
if (request.method == "POST") {
var strBody = "";
request.on("data", function(chunk) {
strBody += chunk;
});
request.on("end", function() {
console.log("Received posted data: " + strBody);
});
} else {
console.dir(request);
}
respone.end(); // for example here
} catch (ex) {
console.dir(ex);
}
};
var app = http.createServer(defaultHandler);
app.listen(8080);
Here you can find documentation - https://nodejs.org/api/http.html#http_class_http_serverresponse
I'm not a Java developer, but I think you can try use flush and getResponseCode method on connection.
conn.setDoOutput(true);
DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(strPostData);
dos.flush();
dos.close();
int responseCode = conn.getResponseCode();
Fixed, it was the Java, I modified the Java code as follows:
URL url = new URL("http://localhost:8080");
conn = (HttpURLConnection)url.openConnection();
if ( conn != null ) {
//Whatever you wants to post...
String strPostData = "p1=Hello&p2=" + (new Date()).getTime();
conn.setRequestMethod("POST");
conn.setRequestProperty("User-Agent", USER_AGENT);
conn.setRequestProperty("Accept-Language", "en-GB,en;q=0.5");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-length", Integer.toString(strPostData.length()));
conn.setRequestProperty("Content-Language", "en-GB");
conn.setRequestProperty("charset", "utf-8");
conn.setUseCaches(false);
conn.setDoOutput(true);
DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(strPostData);
dos.flush();
dos.close();
int intResponse = conn.getResponseCode();
System.out.println("\nSending 'POST' to " + url.toString() +
", data: " + strPostData + ", rc: " + intResponse);;
}

Facebook login with HTTPSUrlConnection pure, without an API

Some way to save tokens without a Facebook API? I want to login on Facebook using native Java libraries. As Mozilla does!
If I set my correct login infos, the 'facebook.com' returns the home page. If I set incorrectly logins, he returns a page of 'you email or password is incorr...'
Why he do not returns my home page, with a friends and etc... ?
Here is the code:
String httpsURL = "https://www.facebook.com/login.php";
String query;
try {
String MY_MAIL = "YOUR MAIL";
String MY_PASS = "YOUR PASS";
query = "lsd=AVpivkhO&email=" + URLEncoder.encode(MY_MAIL, "UTF-8") + "&pass=" + URLEncoder.encode(MY_PASS, "UTF-8") + "&default_persistent=0&timezone=180&lgndim=eyJ3IjoxMzY2LCJoIjo3NjgsImF3IjoxMzY2LCJhaCI6NzM4LCJjIjoyNH0%3D&lgnrnd=135513_yy32&lgnjs=1441832114&locale=pt_BR&qsstamp=W1tbMywxMSwyOSwzMSw1Niw3Miw5NywxNDYsMTUxLDE2OSwxNzEsMTkwLDE5NSwyMzcsMjU1LDI2NiwyODEsMjk0LDMwMSwzMDgsMzM3LDM0MSwzNDYsMzk0LDQxMyw0MzEsNDUyLDQ1NCw0NzIsNDc0LDQ3NSw0OTAsNTMzLDU1MCw1NTYsNTgxLDU4NCw2NDYsNjQ5LDY4NCw3OTMsODUzXV0sIkFabG1OQ2l1WFBTS194bWlmUndYdWlrMVFYT25MODBLMGhoTnpjSmxRTXlGd3lZR0o0ckZZRXp6ZjkwX201aUxuZTJ3VUxsTEdtTW90NExSUzI1LTZyekV6U0RFRE1DWHFuRU11RllZSGstaGE3bTE5UWtRMk5xem9WT3pSVXFNdHBJZU9LNkYzV1EyNFlzVnJhNi16NUVrMzJFOTZGYy1RUDRpTUZic1dGVHRSMzFsY0hRX3l4eXZPdm90ZEhEdXM1UmphcFc5UENVSFNoYmp5RmFNZGxGSyJd";
//query = "";
URL myurl = new URL(httpsURL);
HttpsURLConnection con = (HttpsURLConnection)myurl.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Host", "facebook.com");
con.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0;Windows98;DigExt)");
con.setRequestProperty("Content-Type","Content-Type: application/x-www-form-urlencoded");
con.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
con.setRequestProperty("Accept-Language", "pt-BR,pt;q=0.8,en-US;q=0.5,en;q=0.3");
//con.setRequestProperty("Accept-Encoding", "gzip, deflate");
con.setRequestProperty("Cookie:", "fr=0DlGgNm7j7OSrxwX6.AWVtGD2R_8r7u9v6SmEp-u_cTWA.BVmtC5.sI.AAA.0.AWV7nB8J; lu=RAJEHG-pWh91KhTp4yKcf40A; datr=n9CaVRge8gdmQM4fbYPCgerZ; locale=pt_BR; a11y=%7B%22sr%22%3A0%2C%22sr-ts%22%3A1441880544006%2C%22jk%22%3A0%2C%22jk-ts%22%3A1441880544006%2C%22kb%22%3A1%2C%22kb-ts%22%3A1441880544006%2C%22hcm%22%3A0%2C%22hcm-ts%22%3A1441880544006%7D; reg_fb_ref=https%3A%2F%2Fwww.facebook.com%2F%3Fstype%3Dlo%26jlou%3DAfeJdlzS3ToJP2-zmWuo749IArqE_LKyQPhyMJUZGzXJ04e7NsDHRllozvz1i-L_gVMnR55t3_-GIBPa9s1jrq1eNyoO43bYSUV4YqOC04TPGg%26smuh%3D50695%26lh%3DAc9epmfRhrK0i_WU; reg_fb_gate=https%3A%2F%2Fwww.facebook.com%2F%3Fstype%3Dlo%26jlou%3DAfeJdlzS3ToJP2-zmWuo749IArqE_LKyQPhyMJUZGzXJ04e7NsDHRllozvz1i-L_gVMnR55t3_-GIBPa9s1jrq1eNyoO43bYSUV4YqOC04TPGg%26smuh%3D50695%26lh%3DAc9epmfRhrK0i_WU; wd=1525x268");
con.setRequestProperty("Connection", "keep-alive");
con.setDoInput(true);
con.setDoOutput(true);
DataOutputStream output = new DataOutputStream(con.getOutputStream());
output.writeBytes(query);
output.close();
DataInputStream input = new DataInputStream( con.getInputStream() );
for( int c = input.read(); c != -1; c = input.read()){
System.out.print( (char)c );
}
System.out.println();
System.out.println(">> " + con.getHeaderField("Set-Cookie"));
input.close();
System.out.println("Resp Code:"+con .getResponseCode());
System.out.println("Resp Message:"+ con .getResponseMessage());
//System.out.println("coo: " + coo.get(1));
} catch (UnsupportedEncodingException | MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I'm using Mozilla to analyze the Network HTTP Headers/Requests and Standards, and to get Cookies.
Further questions:
1 - The browser generates the Cookie?
2 - Who first sends cookies, client or server?
3 - What is needed for a successful login? Tokens, cookies... ?
OBS: I already know programming, I'm a good programmer. This is a matter of protocol and standardization. So please respect my doubt and the knowledge.
________________________ EDIT ____________________
This code is similar to mine code. I wanted to do something in that style: mkyong
_______________________ EDIT 2 _____________________
I do not want to use API, but the HTMLUnit looks interesting and no frills.
Looks: How to log into Facebook programmatically using Java?

getting Blank response from php server while access from other country in android

I made a andoid application in which I am getting response from php server, requesting in POST format. I am using below code for http connection:
// random string as boundary for multi-part http post
String strBoundary = "3i2ndDfv2rTHiSisAbouNdArYfORhtTPEefj3q2f";
String endLine = "\r\n";
OutputStream os;
String url = encodeUrl(methodType, graphURL, params);
Log.d("url - ",url);
Log.d("Method Type - ", methodType + " URL - " + url);
HttpURLConnection conn = (HttpURLConnection) new URL(url)
.openConnection();
conn.setConnectTimeout(60000);
conn.setReadTimeout(30000);
conn.setRequestProperty("User-Agent", System.getProperties()
.getProperty("http.agent"));
if (!methodType.equals("GET")) {
Bundle dataparams = new Bundle();
for (String key : params.keySet()) {
if (params.getByteArray(key) != null) {
dataparams.putByteArray(key, params.getByteArray(key));
}
}
// use method override
if (!params.containsKey("method")) {
params.putString("method", methodType);
}
if (params.containsKey("access_token")) {
#SuppressWarnings("deprecation")
String decoded_token = URLDecoder.decode(params
.getString("access_token"));
params.putString("access_token", decoded_token);
}
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type",
"multipart/form-data;boundary=" + strBoundary);
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestProperty("Connection", "Keep-Alive");
conn.connect();
os = new BufferedOutputStream(conn.getOutputStream());
os.write(("--" + strBoundary + endLine).getBytes());
os.write((encodePostBody(params, strBoundary)).getBytes());
os.write((endLine + "--" + strBoundary + endLine).getBytes());
if (!dataparams.isEmpty()) {
for (String key : dataparams.keySet()) {
os.write(("Content-Disposition: form-data; filename=\""
+ key + "\"" + endLine).getBytes());
os
.write(("Content-Type: content/unknown" + endLine + endLine)
.getBytes());
os.write(dataparams.getByteArray(key));
os.write((endLine + "--" + strBoundary + endLine)
.getBytes());
}
}
os.flush();
}
String response = "";
try {
response = parseXmlSvn(conn.getInputStream());
} catch (FileNotFoundException e) {
// Error Stream contains JSON that we can parse to a FB error
//response = read(conn.getErrorStream());
Log.i("RESPONSE",""+response);
}
Log.i("RESPONSE", ""+response);
return response;
Everything is working fine on my demo server, I uploaded my PHP service to clients server. I am getting proper response from his server also.
But when client access app in 3G or 4G in some other country in his android devices, he is getting Blank response while the same service of same server can be accessible in iOS code easily by him. Is it some packet loss while getting response or any other problem?
I would really appreciate any help you can give me.
Thanks

jsp HTTPS Post Request

I am trying to send HTTPS Post Request from jsp to Opera Server. But I can't. I have found many HTTP Post Request examples but no HTTPS. I am getting an Error Message now..
java.lang.ClassCastException: sun.net.www.protocol.https.HttpsURLConnectionImpl cannot be cast to com.sun.net.ssl.HttpsURLConnection
My import are as follows for this code
java.security.Security, com.sun.net.ssl.
My code as follows
try{
String data = URLEncoder.encode("AccountID", "UTF-8") + "=" + URLEncoder.encode(accountid, "UTF-8");
data += "&" + URLEncoder.encode("CallerTransactionID", "UTF-8") + "=" + URLEncoder.encode(callertransactionid, "UTF-8");
data += "&" + URLEncoder.encode("CurrentTime", "UTF-8") + "=" + URLEncoder.encode(currenttime, "UTF-8");
data += "&" + URLEncoder.encode("ErrorURL", "UTF-8") + "=" + URLEncoder.encode(errorurl, "UTF-8");
//data += "&" + URLEncoder.encode("FrameURL", "UTF-8") + "=" + URLEncoder.encode(frameurl, "UTF-8");
System.setProperty("java.protocol.handler.pkgs", "com.sun.net.ssl.internal.www.protocol");
java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
URL opxurl = new URL("https://opx-test.opera.com/opx/2.0/OPXPaymentEnable");
HttpsURLConnection connopx = (HttpsURLConnection) opxurl.openConnection();
connopx.setDoInput(true);
connopx.setDoOutput(true);
connopx.setRequestMethod("POST");
connopx.setFollowRedirects(true);
connopx.setRequestProperty("Content-length",String.valueOf(data.length()));
connopx.setRequestProperty("Content-Type","application/x-www- form-urlencoded");
// open up the output stream of the connection
DataOutputStream output = new DataOutputStream(connopx.getOutputStream());
// write out the data
int dataLength = data.length();
output.writeBytes(data);
//output.flush();
System.out.println("HOly Portal ResponseDesc Code:"+connopx.getResponseCode()+" : "+connopx.getResponseMessage());
// get ready to read the response from the cgi script
DataInputStream input = new DataInputStream( connopx.getInputStream() );
// read in each character until end-of-stream is detected
for( int c = input.read(); c != -1; c = input.read() )
System.out.print("HolyPortal respnse: "+ (char)c );
input.close();
After all connopx's settings you have to call
connopx.connect();
I use this import and it works in my code:
import javax.net.ssl.HttpsURLConnection;
I don't have the two lines above URL opxurl =
Have a look at this answer with similar issue also.
EDIT:
To avoid error 500 remove space in "application/x-www-form-urlencoded" and add those two lines right after output.writeBytes(data);:
output.flush();
output.close();
Then it should work.
This should be your code
try {
URL url = new URL("yourURL");
URLConnection conn = url.openConnection();
if (conn instanceof HttpsURLConnection) {
// Try again as HTTPS
HttpsURLConnection conn1 = (HttpsURLConnection) url.openConnection();
conn1.setHostnameVerifier(new HostnameVerifier() {
#Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
conn1.getResponseCode();
}
}
catch (MalformedURLException e) {
}
catch (IOException e) {
}

Categories

Resources