{"id":"500954d40d7dc143","repo":"square/okhttp","slug":"unexpected-code-500954","errorCode":null,"errorMessage":"Unexpected code ","messagePattern":"Unexpected code ","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"samples/guide/src/main/java/okhttp3/recipes/SynchronousGet.java","lineNumber":33,"sourceCode":" */\npackage okhttp3.recipes;\n\nimport java.io.IOException;\nimport okhttp3.Headers;\nimport okhttp3.OkHttpClient;\nimport okhttp3.Request;\nimport okhttp3.Response;\n\npublic final class SynchronousGet {\n  private final OkHttpClient client = new OkHttpClient();\n\n  public void run() throws Exception {\n    Request request = new Request.Builder()\n        .url(\"https://publicobject.com/helloworld.txt\")\n        .build();\n\n    try (Response response = client.newCall(request).execute()) {\n      if (!response.isSuccessful()) throw new IOException(\"Unexpected code \" + response);\n\n      Headers responseHeaders = response.headers();\n      for (int i = 0; i < responseHeaders.size(); i++) {\n        System.out.println(responseHeaders.name(i) + \": \" + responseHeaders.value(i));\n      }\n\n      System.out.println(response.body().string());\n    }\n  }\n\n  public static void main(String... args) throws Exception {\n    new SynchronousGet().run();\n  }\n}\n","sourceCodeStart":15,"sourceCodeEnd":48,"githubUrl":"https://github.com/square/okhttp/blob/4fc083138014aba3d0078f5c26d1ce84815fa984/samples/guide/src/main/java/okhttp3/recipes/SynchronousGet.java#L15-L48","documentation":"SynchronousGet.java:33 throws 'Unexpected code ' whenever the plain GET to https://publicobject.com/helloworld.txt returns a non-2xx status. This is the most minimal OkHttp recipe and the guard is intentionally pedagogical — it demonstrates the canonical 'check isSuccessful() before reading body' pattern. OkHttp itself never throws this; it is application code in the sample.","triggerScenarios":"The demo host publicobject.com is unavailable (502/503/504) or the path /helloworld.txt has been moved/deleted (404). A captive portal returning 302/200-HTML breaks the assertion conceptually but stays 'successful' on code; an upstream proxy returning 407 auth-required, or Cloudflare challenging with 403/503, trips the throw.","commonSituations":"Running the bare-bones sample offline or behind a corporate proxy; DNS resolution succeeding but the origin returning a maintenance page; certificate mismatch causing an earlier SSLPeerUnverifiedException (distinct error); hotlink protection returning 403.","solutions":["Check network connectivity and confirm the URL still resolves in a browser; switch the recipe to a stable endpoint (e.g. https://raw.githubusercontent.com/.../helloworld.txt) if publicobject.com is down.","Inspect response.code() and response.headers() before throwing — at minimum log Server, Content-Type, and the body.","If behind a proxy, configure OkHttpClient with a Proxy and Authenticator (proxy auth) so the 407 turns into a 200.","Handle redirects explicitly via .followRedirects(true) (default) and check response.priorResponse() if a redirect chain lands on an error."],"exampleFix":"// before\ntry (Response response = client.newCall(request).execute()) {\n  if (!response.isSuccessful()) throw new IOException(\"Unexpected code \" + response);\n  ...\n}\n\n// after\ntry (Response response = client.newCall(request).execute()) {\n  if (!response.isSuccessful()) {\n    throw new IOException(request.url() + \" -> \" + response.code()\n        + \" \" + response.header(\"Server\"));\n  }\n  ...\n}","handlingStrategy":"try-catch","validationCode":"// Cheap liveness probe before the real GET\nRequest head = new Request.Builder().url(url).head().build();\ntry (Response r = client.newCall(head).execute()) {\n  if (!r.isSuccessful()) {\n    throw new IllegalStateException(url + \" not fetchable: \" + r.code());\n  }\n}","typeGuard":null,"tryCatchPattern":"try (Response response = client.newCall(request).execute()) {\n  if (!response.isSuccessful()) {\n    throw new IOException(request.url() + \" -> \" + response.code()\n        + \" \" + response.header(\"Server\"));\n  }\n  consume(response.body().string());\n} catch (java.net.UnknownHostException | java.net.SocketTimeoutException e) {\n  // network-level failure, distinct from a non-2xx response\n  throw new IOException(\"cannot reach \" + request.url(), e);\n}","preventionTips":["Point samples at a stable, owned endpoint rather than publicobject.com.","Configure OkHttpClient.connectTimeout/readTimeout so unreachable hosts fail fast instead of hanging.","If behind a corporate proxy, set Proxy and proxyAuthenticator.","Always read the response body in catch via peekBody for diagnostics before closing."],"tags":["okhttp","java","http-status","synchronous","server-outage"],"analyzedSha":"4fc083138014aba3d0078f5c26d1ce84815fa984","analyzedAt":"2026-08-04T19:09:04.639Z","schemaVersion":2}