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
Related
I followed this guide to create my own REST API. I am trying to consume my API that I built from the guide but I ran into some trouble when it came to using any request that wasn't a GET request. When I tried doing a delete request. (http://localhost:8080/api/v1/employees/3)
I would get a 405 error and I'm not sure why (I do not have any password protection in my local host). I want to understand how I can create requests other than GET. I tried using query parameters for my POST request, but it was unsuccessful.
I looked at all the other StackOverFlow Similar Questions and I couldn't find anything.
EDIT1: I am using a simple Java Application to do this.
This was the code I used in order to do my GET requests
String urlString = "http://localhost:8080/api/v1/employees";
try {
String result = "";
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
BufferedReader rd = new BufferedReader (new InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
result += line;
}
rd.close();
System.out.println(result);
}
Try to replace this URLConnection conn = url.openConnection();
to this:
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
you can use org.springframework.web.client.RestTemplate (rest-template) to consume rest api.
for delete, you can do something like
private void deleteEmployee() {
Map < String, String > params = new HashMap < String, String > ();
params.put("id", "1");
RestTemplate restTemplate = new RestTemplate();
restTemplate.delete(DELETE_EMPLOYEE_ENDPOINT_URL, params);
}
please check https://www.javaguides.net/2019/06/spring-resttemplate-get-post-put-and-delete-example.html and https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/client/RestTemplate.html and https://www.baeldung.com/rest-template
hope these provide enough info
I am doing an desktop application using Java that can send SMS to a multiple phone numbers via browser. To send the SMS, I need to put in parameters that username, password, message to be sent and the recipient's number.
I have used this and it worked but the problem is it opens up a browser to every recipient so it is not advisable especially if its going to be sent to a lot:
Runtime rt = Runtime.getRuntime();
String url = String.format("http://www.companysms.com/RemoteAPI/SendSMS.aspx?username=%s&encoding=url&password=%s&messagedata=%s&receiver=%s&binary=0", txtUsername.getText(),txtPassword.getText(), txtMessage.getText(),tblMessage.getValueAt(i, 2));
rt.exec("rundll32 url.dll,FileProtocolHandler "+ url);
I have tried this one too but it gives me the HTTP response code 400:
HttpURLConnection connection = (HttpURLConnection) myURL.openConnection();
connection.setRequestMethod("GET");
connection.setDoOutput(true);
connection.connect();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder results = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
results.append(line);
}
connection.disconnect();
If I use the URLencoder with "UTF-8", it makes it worse because i cant be sure of what the message the user will send and if they will use any of the special characters. Is there a way to have the first set of codes that I used to not open browser after sending the SMS or is there another way of doing this? Any help is appreciated. Thanks!
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
hi guys i want to code a tool, which interacts with phpmyadmin. I want to create a new Table. For this i need the cookie and the security token. Both things I've done. My Problem is i'll take the cookies and open an new URLConnection with these Cookies. And take the token to validate my request. But everytime i do this i got the response that my SQLQuery is empty and if u get this Error ur token is invalid. And an invalid token means that ur cookies haven't been placed very well in the new connection so u don't have the same session as before. What i've done wrong, any idea to fix this problem?
Here is my code:(its very ugly but its only for testing purposes)
import java.io.*;
import java.net.*;
import java.util.List;
public class connect {
public void connectto() throws IOException{
URLConnection connection = new URL("http://localhost/phpmyadmin/index.php").openConnection();
List<String> cookies = connection.getHeaderFields().get("Set-Cookie");
connection.connect();
InputStream response = connection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(response));
String line;
String token = "";
while((line = br.readLine())!= null){
System.out.println(line);
if (line.contains("var token = ")){
System.out.println("hit");
token = "&token=" + line.substring(line.indexOf("var token = '") + "var token = '".length()).substring(0, line.substring(line.indexOf("var token = '") + "var token = '".length()).indexOf("';"));
}
}
System.out.println(token);
String url = URLEncoder.encode("db=mysql&sql_query=CREATE TABLE testtable(testtable TEXT);" + token, "UTF-8");
connection = new URL("http://localhost/phpmyadmin/sql.php?" + url).openConnection();
connection.setDoOutput(true);
for (String cookie : cookies) {
System.out.println(cookie.split(";", 2)[0]);
connection.addRequestProperty("Cookie", cookie.split(";", 2)[0]);
}
connection.connect();
response = connection.getInputStream();
br = new BufferedReader(new InputStreamReader(response));
line = "";
while((line = br.readLine())!= null){
System.out.println(line);
}
}
}
My apologies if I misunderstood your issue but why are you forcing the request through phpMyAdmin? The point of phpMyAdmin is to provide a UI to people to work with their MySQL database. If you want to interact with the MySQL database you should be opening a connection directly to the database and executing statements in the java code.
Again, please excuse my ignorance if this is unfeasable for you but if you must work through phpMyAdmin instead of a direct connection between Java and your DB please provide more information.