Catching System.Net.WebException: (502) Bad Gateway.

Topics: User Forum
Jun 20, 2014 at 9:05 AM
Edited Jun 20, 2014 at 9:08 AM
Hi there,

first of all, I love the FlickrNet Library. It is straight forward and very easy to use - thanks a lot to everybody involved!

I am having a problem with my program and maybe one of you knows an answer:
Occasionally, returns an Exception "(502) Bad Gateway.". I believe it is because I am using the Try...Catch...Finally Statement that I am able to see this error at all.
However, I would like my program to continue processing - if 502-Error occurs, it should just try repeating the last request and go on.

How can I catch this specific Error in VBNet?

This is the full error message:
See the end of this message for details on invoking 
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.Net.WebException: WebException occurred with the following body content: <!DOCTYPE html>
<html lang="en-us"><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,minimal-ui">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
html {
    height: 100%;
body {
    background: #fafafc url( 50% 50%;
    background-size: cover;
    height: 100%;
    text-align: center;
    font: 300 18px "helvetica neue", helvetica, verdana, tahoma, arial, sans-serif;
table {
    height: 100%;
    width: 100%;
    table-layout: fixed;
    border-collapse: collapse;
    border-spacing: 0;
    border: none;
h1 {
    font-size: 42px;
    font-weight: 400;
    color: #400090;
p {
    color: #1A1A1A;
#message-1 {
    font-weight: bold;
    margin: 0;
#message-2 {
    display: inline-block;
    *display: inline;
    zoom: 1;
    max-width: 17em;
    _width: 17em;
<!-- status code : 502 -->
<!-- Server Connection Closed -->
<!-- host machine: -->
    <img src="" alt="Yahoo Logo">
    <h1 style="margin-top:20px;">Will be right back...</h1>
    <p id="message-1">Thank you for your patience.</p>
    <p id="message-2">Our engineers are working quickly to resolve the issue.</p>

 ---> System.Net.WebException: The remote server returned an error: (502) Bad Gateway.
Jun 20, 2014 at 9:46 AM
You just have to wrap your code in a loop, and have a Catch(exception As WebException) statement (if I got my VB.Net correct).

Jun 21, 2014 at 1:45 PM
Edited Jun 21, 2014 at 1:45 PM
Thank you, Sam. I had a Try.. Catch.. Loop, but it somehow did not catch the error. I revised this part of code and it now looks like this:
            Dim Photos As PhotoCollection = f.PhotosSearch(options)
        Catch ex As FlickrException
            Outputtextbox.Text &= "Error occured: " & ex.ToString
            GoTo repeat
        End try
Until now, no Webexception occurred, but I will write here if it worked out.. . Thanks a lot for your advice!
Jun 21, 2014 at 2:38 PM
Edited Jun 21, 2014 at 3:57 PM
Hm, this did not work.. I still get the error 502 and my program aborts:

Is it possible that this exception is not part of FlickrException?

Of course.. its System.Net.WebException and not FlickrException!

Too tired.. ;) Problem solved. Thank you!