URLConnection working very slow when using proxy - java

It took me a long time to get the proxy for my Java connection working at my office. Now I got it working, but it's VERY slow.
I use the following code:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Authenticator;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.PasswordAuthentication;
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;
public class Test {
public static void main(String[] args) {
long millis = System.currentTimeMillis();
URL url;
InputStream is = null;
BufferedReader br;
String line;
try {
Authenticator authenticator = new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return (new PasswordAuthentication("xxxx",
"xxxx".toCharArray()));
}
};
Authenticator.setDefault(authenticator);
url = new URL("http://stackoverflow.com/");
URLConnection conn = url.openConnection(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("xxx", xxx)));
is = conn.getInputStream();
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
}
}
System.out.println("Duration: "+(System.currentTimeMillis()-millis));
}
}
When I run this, the output is the following:
<html><head><title>Object moved</title></head><body>
<h2>Object moved to here.</h2>
</body></html>
Duration: 222856
As you can see, the proxy is working. I do get the webpage just fine. But it's waiting very long before showing the webpage contents (more than 222 seconds :O). I have no clue what could possibly be the problem here.
Just some information about the environment: I'm working on my office VDI. Google Chrome and IE have internet connection, but other programs don't. By a lot of googling I found the correct proxy settings.

Related

How to perform post request in header and body in JSON

I'm using JSON and want to send post request to server via username, password in body and x-auth-app-id, x-auth-app-hash in header..
I have test on Postmen and it return 200 (status ok), But when I build my sources it happen error.
This is my class header:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.URL;
import net.sf.json.JSONObject;
public class HttpRequestUtil {
public static JSONObject httpRequest(String requestUrl, String requestMethod, String outputStr) {
JSONObject jsonObject = null;
StringBuffer buffer = new StringBuffer();
InputStream inputStream=null;
try {
URL url = new URL(requestUrl);
HttpURLConnection httpUrlConn = (HttpURLConnection) url.openConnection();
httpUrlConn.setDoOutput(true);
httpUrlConn.setDoInput(true);
httpUrlConn.setUseCaches(false);
httpUrlConn.setRequestProperty("x-auth-app-id", "6166611659356156223");
httpUrlConn.setRequestProperty("x-auth-app-hash", "a44f4ea21475fa6761392ba4bc659990bee771c413b2c207490a79f9ec78c2a61234");
httpUrlConn.setRequestProperty("Content-Type", "application/json");
httpUrlConn.setRequestMethod(requestMethod);
if ("POST".equalsIgnoreCase(requestMethod))
httpUrlConn.connect();
if (null != outputStr) {
OutputStream outputStream = httpUrlConn.getOutputStream();
outputStream.write(outputStr.getBytes("UTF-8"));
outputStream.close();
}
inputStream = httpUrlConn.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String str = null;
while ((str = bufferedReader.readLine()) != null) {
buffer.append(str);
}
bufferedReader.close();
inputStreamReader.close();
inputStream.close();
inputStream = null;
httpUrlConn.disconnect();
jsonObject = JSONObject.fromObject(buffer.toString());
}
catch (ConnectException ce) {
ce.printStackTrace();
System.out.println("Our server connection timed out");
}
catch (Exception e) {
e.printStackTrace();
System.out.println("https request error:{}");
}
finally {
try {
if(inputStream!=null) {
inputStream.close();
}
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return jsonObject;
}
}
And class Body:
import java.util.UUID;
import java.util.Map;
import java.util.HashMap;
import java.util.Formatter;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.io.UnsupportedEncodingException;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
public class CallCenterController {
public static void main(String[] args) throws JSONException {
String sipUser = "vchi_dd";
String sipPassword = "m9Bp7s+CtQj85HygnIFjPn7O4Vithrunaa";
Map<String, Object> sipAccount = new HashMap<String, Object>();
sipAccount.put("sipUser", sipUser);
sipAccount.put("sipPassword", sipPassword);
sipAccount = postData(sipUser, sipPassword);
System.out.println("result: " + sipAccount);
};
public static JSONObject postData(String sipUser, String sipPassword) {
String url="https://myservice.com/oapi/v1/call/click-to-call/02437590555&sipUser="+sipUser+"&sipPassword="+sipPassword;
return HttpRequestUtil.httpRequest(url, "POST", "");
}
}
When I build it happen an exception following as:
java.io.IOException: Server returned HTTP response code: 400 for URL: https://myservice.com/oapi/v1/call/click-to-call/02437590555&sipUser=vchi_dd&sipPassword=m9Bp7s+CtQj85HygnIFjPn7O4Vithrunaa
https request error:{}
result: null
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1876)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1474)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
at com.mypackage.HttpRequestUtil.httpRequest(HttpRequestUtil.java:63)
at com.mypackage.CallCenterController.postData(CallCenterController.java:45)
at com.mypackage.CallCenterController.main(CallCenterController.java:34)
How to send correct data to my url and fix the problem?
I would use Java HTTP Client API if your java version is high enough.
Here's a link to it https://www.baeldung.com/java-9-http-client
I have used it and it feels more maintainable and clear.
Also, it seems that you're sending the request with empty body even though you say in your question that you are sending username and password in body.
And why are you adding username and password to a map if you are not using the map?
sipAccount.put("sipUser", sipUser);
sipAccount.put("sipPassword", sipPassword);

JSON post encoding in Elasticsearch through maven

I want to make insert in java.Maven with http post to Elasticsearch server.
My code is:
package com.server.java.Webping;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class ReadAndWrite extends Server {
public static void main(String[] args) throws IOException {
try {
URL url = new URL("http://192.168.1.126:9200/shakespeare/_bulk?pretty' --data-binary #shakespeare.json");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
File file = new File("shakespeare.json");
FileReader reader = new FileReader(file);
#SuppressWarnings("resource")
BufferedReader br = new BufferedReader(reader);
String input = null;
StringBuilder builder = new StringBuilder();
while(br.readLine( ) != null)
{
String txt = br.readLine( );
builder.append(txt);
}
input = builder.toString();
OutputStream os = (OutputStream) conn.getOutputStream();
os.write(input.getBytes());
os.flush();
if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {
throw new RuntimeException("Failed : HTTP error code : "
+ conn.getResponseCode());
}
BufferedReader br1 = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br1.readLine()) != null) {
System.out.println(output);
}
conn.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
However when I execute the post method, java return me an error:
java.io.IOException: Error writing to server
at sun.net.www.protocol.http.HttpURLConnection.writeRequests(HttpURLConnection.java:699)
at sun.net.www.protocol.http.HttpURLConnection.writeRequests(HttpURLConnection.java:711)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1567)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1474)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)
at com.server.java.Webping.ReadAndWrite.main(ReadAndWrite.java:38)
I have also checked my server connection with pinging socket request and its working .
code for pinging request:
package com.server.java.Webping;
import java.net.InetAddress;
import java.net.Socket;
public class WebPing {
public static void main(String[] args) {
try {
InetAddress addr;
Socket sock = new Socket("192.168.1.126", 9200); // elastic search - 9200 , kibana - 5601
addr = sock.getInetAddress();
System.out.println("Connected to " + addr);
sock.close();
} catch (java.io.IOException e) {
System.out.println("Can't connect to " + args[0]);
System.out.println(e);
}
}
}
and output returns as :
Connected to /192.168.1.126
Can anyone help me out that what is wrong in my code ?

getting the check if twitch stream is live

I have been working with java to make it where i check if a certain user if live and it will say true or false if the user is streaming...im working with minimal json.
here is my code
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import com.eclipsesource.json.JsonObject;
public class hostbot {
public static void main(String[] args) throws Exception {
Twitchbot bot = new Twitchbot();
bot.setVerbose(true);
bot.connect("irc.twitch.tv", 6667, "something");
}
public boolean isStreamLive()
{
try
{
URL url = new URL("https://api.twitch.tv/kraken/streams/rexephon");
URLConnection conn = url.openConnection();
BufferedReader br = new BufferedReader( new InputStreamReader( conn.getInputStream() ));
String inputLine = br.readLine();
br.close();
JsonObject jsonObj = JsonObject.readFrom(inputLine);
return ( jsonObj.get("stream").isNull() )?false:true;
}
catch (IOException e)
{
e.printStackTrace();
}
return false;
}
}
when i return false is that suppose to print in the log the word false? or something else?

when open connection of URL by Java API it is working fine in development side,but not working on UAT,the error is showing Connection timed out

when open connection of URL by Java API it is working fine in development side,but not working on UAT,the error is showing Connection timed out.Please suggest what to do?
Here given userID,password proxyip and port are not actual because it is the client url.
package qc.los.util;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Random;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
import qc.dal.DAL;
import qc.dal.dto.ParameterDTO;
import qc.los.controller.action.NewProspectAction;
import qc.sso.util.UserLockingFilter;
public class Utils {
protected static Logger log = Logger.getLogger(Utils.class);
public static void main(String[] args)
{
Utils Utilss=new Utils();
Utilss.sendVerificationCodeToCustomer("","9811937492","myTestras",true,"properhost","properport");
}
public String sendVerificationCodeToCustomer(String prospectId,String mobileNumber, String verificationCode, boolean proxyEnabled, String proxyHost, String proxyPort)
{
log.info("Start");
Properties systemSettings = System.getProperties();
try
{
//UPdated by for user id password
String urlStr ="http://www.example.com/SendSMS/sendmsg.php?uname=rahul&pass=rahul&send=Tag&dest="+mobileNumber+"&msg=Your%20verification%20code%20is%20"+verificationCode+"&concat=1";
log.info("urlStr "+urlStr);
URL u = new URL (urlStr);
log.info("proxyHost and proxyPort "+proxyHost+" "+proxyPort);
if(proxyEnabled)
{
log.info("proxyEnabled with proxyHost and proxyPort "+proxyHost+" "+proxyPort);
systemSettings.put("proxySet", "true");
systemSettings.put("proxyHost", proxyHost);
systemSettings.put("proxyPort", proxyPort);
}
HttpURLConnection con = (HttpURLConnection) u.openConnection ();
con.setDoInput(true);
con.setRequestMethod("GET");
log.info("Connection start");
con.connect();
log.info("Connection connected");
InputStream is = con.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
StringBuffer response = new StringBuffer();
while((line = rd.readLine()) != null)
{
response.append(line);
response.append("<br>");
}
rd.close();
log.info("End:Message sucessfull with response "+response.toString());
return response.toString();
}
catch(Exception e)
{
String proxySetting = proxyEnabled + ":" + proxyHost + ":" + proxyPort;
e.printStackTrace();
log.error("exception while sending sms:("+proxySetting+")"+" "+e.getMessage());
return "exception while sending sms:("+proxySetting+")"+e.getMessage();
}
finally
{
if(proxyEnabled)
{
systemSettings.remove("proxySet");
systemSettings.remove("proxyHost");
systemSettings.remove("proxyPort");
}
log.info("End");
}
}
}

Tring to connect using HTTPS: Server redirected too many times

I am trying to connect to a secured connection URL (https://example.com ) using a Java program to check availability of the site. Generally, I connect to https://example.com in browser by disabling proxy settings. Also, we have installed certificates in trusted root certificates.
I have added these certificates to Java Keystore successfully.
import java.net.URL;
import java.net.URLConnection;
import java.security.Security.*;
import com.sun.net.ssl.*;
import com.sun.*;
import javax.net.ssl.HttpsURLConnection;
import java.security.cert.Certificate;
import java.io.*;
import javax.net.ssl.SSLPeerUnverifiedException;
import org.omg.CORBA_2_3.portable.InputStream;
public class TestConn {
public static void main(String args [])
{
try{
URL hp = new URL("https://example.com");
HttpsURLConnection hpCon = (HttpsURLConnection)hp.openConnection();
boolean isProxy = hpCon.usingProxy();
System.out.println("is using proxy " + isProxy);
InputStream obj = (InputStream) hpCon.getInputStream();
while(obj.read()!=-1){
System.out.println(obj.read_char());
}
System.out.println("content >> " + obj.toString());
}catch (Exception ex){
ex.printStackTrace();
}
}
}
I have encountered the following error:
java.net.ProtocolException: Server redirected too many times (20)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)
at TestConn.main(TestConn.java:28)
Can anyone please help me regarding this exception?
If you want to check availability of the site, you should use hpCon.getResponseCode();.
Response code 200 means that site is available. Frankly, i don't know your further purpose.
This is the modified codes, sure got the output content.
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
public class Test {
public static void main(String[] args) {
try {
URL hp = new URL("https://godaddy.com");
HttpsURLConnection hpCon = (HttpsURLConnection) hp.openConnection();
boolean isProxy = hpCon.usingProxy();
System.out.println("is using proxy " + isProxy);
InputStream obj = hpCon.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(obj));
String s;
while ((s = br.readLine()) != null) {
System.out.println("content >>" + s);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}

Categories