Site icon FSIBLOG

HTTP 505 From HttpsURLConnection: The Spaces In Your URL Are Breaking the Request

Java HTTP 505 Error Fix: Encode Spaces in the URL

Java HTTP 505 Error Fix: Encode Spaces in the URL

You paste a Yahoo YQL link into the browser, and XML comes back without any trouble. You pass the same link to HttpsURLConnection in Java, and the server answers with 505 HTTP Version Not Supported. The server does support HTTPS, and it does support HTTP/1.1. The fault lies in the address your code sends, though the error code points you somewhere else entirely.

The Code That Returns 505

This is the snippet from the question. At runtime, params[0] held the full YQL address, typed the way a person would read it:

https://query.yahooapis.com/v1/public/yql?q=select * from yahoo.finance.quote where symbol %3D"msft"&diagnostics=true&env=store://datatables.org/alltableswithkeys
java
URL url = new URL(params[0]);
URLConnection connection = url.openConnection();
HttpsURLConnection httpConnect = (HttpsURLConnection) connection;
int responseCode = httpConnect.getResponseCode();   // returns 505

Each line compiles and runs. The problem is in the string, because it still contains raw spaces and double quotes.

A Space Turns Into a “Version” Error

Every HTTP request starts with a single line made of three parts. The HTTP/1.1 message syntax in RFC 9112 defines that line as a method, a single space, the request target, another single space, and the protocol version:

GET /v1/public/yql?q=... HTTP/1.1

Java’s URL class does not encode your address for you. Whatever you hand it goes onto the wire as it is, so the request line comes out like this:

GET /v1/public/yql?q=select * from yahoo.finance.quote where symbol %3D"msft"&... HTTP/1.1

The server reads up to the first space after the path and stops there. Its path is now only /v1/public/yql?q=select. The slot where it expects HTTP/1.1 holds *, and the words after it have nowhere to go.

Diagram of an HTTP request line split by raw spaces into extra parts, with the version slot holding an asterisk, compared with the encoded line that splits into method, path and HTTP/1.1

The raw URL splits into too many parts and leaves * in the version slot. After encoding, the line splits cleanly into three.

Since * is not a valid HTTP version, the server replies 505. The MDN reference for the 505 status lists a malformed request line, such as a path with spaces in it, as one way this error happens. Some servers answer the same mistake with 400 Bad Request instead, so you may see either code depending on who is on the other end.

The Same Link Works in the Browser for a Reason

A browser cleans up an address before sending it. Spaces become %20, and quotes become %22, so the request line stays in three clean parts. If you copy the link back out of the address bar, you will usually see those %20 codes in it. HttpsURLConnection makes no such repairs, so you have to encode the address yourself.

The Fixed Code

Build the address in pieces, and encode only the parameter values. That way the ?, & and = keep their meaning while the spaces and quotes inside the query are made safe.

java
import java.net.URL;
import java.net.URLEncoder;
import javax.net.ssl.HttpsURLConnection;

String yql = "select * from yahoo.finance.quote where symbol = \"msft\"";
String env = "store://datatables.org/alltableswithkeys";

String address = "https://query.yahooapis.com/v1/public/yql"
        + "?q=" + URLEncoder.encode(yql, "UTF-8")
        + "&diagnostics=true"
        + "&env=" + URLEncoder.encode(env, "UTF-8");

URL url = new URL(address);
HttpsURLConnection httpConnect = (HttpsURLConnection) url.openConnection();
httpConnect.setRequestMethod("GET");
int responseCode = httpConnect.getResponseCode();

After encoding, the query value travels as select+*+from+yahoo.finance.quote+where+symbol+%3D+%22msft%22. No raw space is left in it, so the request line keeps its three parts, and the 505 goes away.

Character-by-character view of the YQL query value before and after URLEncoder, with spaces turned into plus signs and quotes and the equals sign turned into percent codes

Each space becomes +, and each quote and equals sign becomes a percent code. The asterisk is a safe character, so it stays as it is.

Four Points to Keep In Mind With This Fix

You Can Spot This Problem in Under a Minute Next Time

A 505 from a server that clearly speaks HTTP/1.1 almost always means the request line got split in the wrong place. Before you change any protocol settings, log the exact string you pass to new URL(), and search it for spaces, quotes or line breaks. A trailing newline read from a file or a text field can cause the same error. If the request still fails after that check, you can send the error to the FSIBlog team with the full stack trace and the exact URL your code built.

This Yahoo Endpoint Was Shut Down in 2019

The fix above solves the 505, though the YQL address in this example will not answer today. Yahoo retired the YQL service at query.yahooapis.com on January 3, 2019, as the Yahoo Query Language entry on Wikipedia records, and services built on it, including the datatables.org tables, stopped working at the same time. If you need stock quotes now, you will have to move to a different finance API. The encoding rule still applies to whichever API you choose, because any query that holds a space or a quote has to be encoded before it is sent.

Exit mobile version