Here is my code in jsp:
<c:catch var="feederror">
<%
String feedUrl = (String) pageContext.getAttribute("url");
String feedXml = "";
HttpURLConnection conn = (HttpURLConnection) new URL(feedUrl).openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
String line;
while ((line = reader.readLine()) != null) {
feedXml += line;
}
reader.close();
pageContext.setAttribute("feedXml", feedXml.trim().replaceAll("",""));
%>
</c:catch>
The variable feederror returns java.net.ProtocolException: Server reditected too many times(20).
I have tried:
to open browser with disabled cookies - page do not load at all (404);
used CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL)) before openning connection - didn't change anything;
tried CookieHandler.setDefault(new CookieManager()) - didn't change anything;
How to solve the problem?
java.net.ProtocolException: Server reditected too many times(20).
This exception comes from HttpURLConnection conn = (HttpURLConnection) new URL(feedUrl).openConnection();
For 99% you're calling an url, which sends a redirect to itself, or site B, which redirect to site A, which redirects to site B etc. so there is a loop.
Check what URL you're trying to call and verify it via some external tool, so you can check headers and responses from external server.
I think it has nothing to do with cookies
I am trying to get users machine ipAddress using java from my server.
I used the following code, and it works fine in local.
URL url = new URL("http://www.geoplugin.net/json.gp?jsoncallback=?");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
BufferedReader in = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
System.out.println(response.toString());
}
}
Using this class, response shows the ip in my local web app.(localhost:8080/myApp/getIp)
I deployed this war to my AWS server, and tried to run the servlet. But this always shows the AWS ip address only. (myIp:8080/myApp/getIp)
It doesn't shows my machine IP.
What was mistake in my code, can someone assist in this?
ServletRequest.getRemoteAddr() OR
getRemoteHost() and getRemotePort()
should returns details of the actual client
If you wanna get client ip i.e. user accessing your web-app through browser, then you can use (client-side-script)javascript to do so. Below is simple example for the same.
function show(response) {
console.log(response);
var html = 'Something went wrong !';
if (200 == response.geoplugin_status) {
html = 'Got your ip as: ' + response.geoplugin_request;
}
document.getElementById('ip').innerHTML = html;
}
var script = document.createElement('script');
script.async = true;
script.src = 'http://www.geoplugin.net/json.gp?jsoncallback=show';
document.getElementById('ip').appendChild(script);
<div id='ip'>Fetching IP ...</div>
I want to do an online xml request in java but the server responds with 401 error that means that there is an authentication that is need to access the server. I have the certfile.cer that i can use to do the authentication but i dont know how to load it in java.How can I achieve this in java? Here is part of my code.
StringBuilder answer = new StringBuilder();
URL url = new URL("www.myurl.com");
URLConnection conn = url.openConnection();
OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
writer.write(xml);
writer.flush();
String line;
while ((line = reader.readLine()) != null)
{
answer.append(line);
}
I have a java program which I want to input something into an html form. If possible it could just load a url like this
.../html_form_action.asp?kill=Kill+Server
But i'm not sure how to load a url in Java. How would I do this? Or is there a better way to send an action to an html form?
Depending on your security, you can make an HTTP call in Java. It is often referred to as a RESTFul call. The HttpURLConnection class offers encapsulation for basic GET/POST requests. There is also an HttpClient from Apache.
Here's how you can use URLConnection to send a simple HTTP request.
URL url = new URL(url + "?" + query);
// set connection properties
URLConnection connection = url.openConnection();
connection.setRequestProperty("Accept-Charset", "UTF-8");
connection.connect(); // send request
// read response
BufferedReader reader = new BufferedReader(
new InputStreamReader(connection.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
reader.close(); // close connection
I am trying to send arabic sms (or french sms) from kannel and it does not get the client end perfectly ( somtimes ?????? for arabic words),
after doing some analysis on the values sent between the bearebox,smsbox and smsc i found out that the error in encoding happens between my bearbox and smsbox.
I have used charset=utf-8&coding=2 on my http request for sending sms but the same problem occurs
Does anyone have any idea on what is the problem or better a solution ??
here is the code i use to send sms
StringBuffer param = new StringBuffer()
param.append("http://localhost:1025/cgi-bin/sendsms?")
param.append(URLEncoder.encode("username","UTF-8")).append("=").append(URLEncoder.encode("xxx","UTF-8"))
param.append("&").append(URLEncoder.encode("password","UTF-8")).append("=").append(URLEncoder.encode("xxxx","UTF-8"))
param.append("&").append(URLEncoder.encode("to","UTF-8")).append("=").append(URLEncoder.encode(numTel,"UTF-8"));
param.append("&").append(URLEncoder.encode("charset","UTF-8")).append("=").append(URLEncoder.encode("utf-8","UTF-8"))
param.append("&").append(URLEncoder.encode("coding","UTF-8")).append("=").append(URLEncoder.encode("2","UTF-8"))
param.append("&").append(URLEncoder.encode("text","UTF-8")).append("=").append(URLEncoder.encode(text,"UTF-8"))
param.append("&").append(URLEncoder.encode("priority","UTF-8")).append("=").append(URLEncoder.encode(""+priority,"UTF-8"))
param.append("&").append(URLEncoder.encode("dlr-mask","UTF-8")).append("=").append(URLEncoder.encode("31","UTF-8"))
param.append("&").append(URLEncoder.encode("dlr-url","UTF-8")).append("=").append(URLEncoder.encode(urlString,"UTF-8"))
try{
URL url = new URL(param.toString())
System.out.println("INFO : Opening connection ")
HttpURLConnection urlconnection = (HttpURLConnection) url.openConnection()
System.out.println("INFO : Connection openned")
BufferedReader input = new BufferedReader( new InputStreamReader(urlconnection.getInputStream()))
String inputLine
while ((inputLine = input.readLine()) != null)
aResult.append(inputLine)
input.close()
}catch(Exception e){
e.printStackTrace()
return false
}
System.out.println("response : "+aResult.toString())
System.out.println("INFO : all sent disconnect.")
'
Thank you
i have been able to send arabic/french sms with this http request example :
StringBuffer param = new StringBuffer()
param.append("http://localhost:1025/cgi-bin/sendsms?")
param.append(URLEncoder.encode("username","UTF-8")).append("=").append(URLEncoder.encode("xxx","UTF-8"))
param.append("&").append(URLEncoder.encode("password","UTF-8")).append("=").append(URLEncoder.encode("xxxx","UTF-8"))
param.append("&").append(URLEncoder.encode("to","UTF-8")).append("=").append(URLEncoder.encode(numTel,"UTF-8"));
param.append("&").append(URLEncoder.encode("coding","UTF-8")).append("=").append(URLEncoder.encode("2","UTF-8"))
param.append("&").append(URLEncoder.encode("text","UTF-8")).append("=").append(URLEncoder.encode(text,"UTF-8"))
param.append("&").append(URLEncoder.encode("priority","UTF-8")).append("=").append(URLEncoder.encode(""+priority,"UTF-8"))
param.append("&").append(URLEncoder.encode("dlr-mask","UTF-8")).append("=").append(URLEncoder.encode("31","UTF-8"))
param.append("&").append(URLEncoder.encode("dlr-url","UTF-8")).append("=").append(URLEncoder.encode(urlString,"UTF-8"))
try{
URL url = new URL(param.toString())
System.out.println("INFO : Opening connection ")
HttpURLConnection urlconnection = (HttpURLConnection) url.openConnection()
System.out.println("INFO : Connection openned")
BufferedReader input = new BufferedReader( new InputStreamReader(urlconnection.getInputStream()))
String inputLine
while ((inputLine = input.readLine()) != null)
aResult.append(inputLine)
input.close()
}catch(Exception e){
e.printStackTrace()
return false
}
System.out.println("response : "+aResult.toString())
System.out.println("INFO : all sent disconnect.")
i don't send the "charset" parameter in my request i only send the coding parameter
I've made a bit of research, and I believe that if you specify coding=2 Kannel expects the message body encoded as UTF-16, at least if I understood correctly this discussion.
Also you should have a look at this document.
All you have to do is the following (kannel release 1.5):
1- Add alt-charset="UTF-8" under smsc group in your kannel configuration
2- when sending the sms مرحبا for example you have to type type the following in your sendsms command: text=مرحبا&charset=UTF-8&coding=1
Check your database using mysql command because database management tools like webmin may not recognize Arabic letters