Click here to Skip to main content
15,904,155 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: We gonna get the client, finally! Pin
Garth J Lancaster13-Jun-16 17:59
professionalGarth J Lancaster13-Jun-16 17:59 
GeneralRe: We gonna get the client, finally! Pin
Super Lloyd13-Jun-16 18:13
Super Lloyd13-Jun-16 18:13 
GeneralRe: We gonna get the client, finally! Pin
BillWoodruff13-Jun-16 18:23
professionalBillWoodruff13-Jun-16 18:23 
GeneralRe: We gonna get the client, finally! Pin
Super Lloyd13-Jun-16 20:47
Super Lloyd13-Jun-16 20:47 
GeneralRe: We gonna get the client, finally! Pin
Kornfeld Eliyahu Peter13-Jun-16 20:07
professionalKornfeld Eliyahu Peter13-Jun-16 20:07 
GeneralRe: We gonna get the client, finally! Pin
Super Lloyd13-Jun-16 20:47
Super Lloyd13-Jun-16 20:47 
GeneralRe: We gonna get the client, finally! Pin
lmoelleb13-Jun-16 20:47
lmoelleb13-Jun-16 20:47 
GeneralRe: We gonna get the client, finally! Pin
Super Lloyd13-Jun-16 20:52
Super Lloyd13-Jun-16 20:52 
lmoelleb wrote:
Web API:
An inexperience programmer can make a client in three days.... An experienced programmer can make on in ... three days. Hurray for typing client data objects manually.

Really that much time?! Blush | :O
I though 2 minute would be plenty enough!

Here is a WebAPI client helper class for ya! Wink | ;)
C#
public class WebQueryBuilder
{
    StringBuilder url = new StringBuilder();
    bool hasParam;

    public WebQueryBuilder(string url)
    {
        this.url = new StringBuilder(url);
        hasParam = url.Contains("?");
        Method = HttpMethod.Get;
    }

    public void SetX6509Cert(byte[] rawData, string password)
    {
        cert = new X509Certificate2(rawData, password);
    }
    X509Certificate2 cert;

    // Query parameter initialization syntax
    public string this[string key] { set { Add(key, value); } }
    public void Add(string key, string value)
    {
        if (hasParam)
        {
            url.Append("&");
        }
        else
        {
            url.Append("?");
            hasParam = true;
        }
        url.Append(WebUtility.UrlEncode(key));
        url.Append("=");
        url.Append(WebUtility.UrlEncode(value));
    }
    public void AddRange(ICollection<KeyValuePair<string, string>> headers)
    {
        if (headers != null)
            foreach (var kv in headers)
                Add(kv.Key, kv.Value);
    }

    public HttpMethod Method { get; set; }

    /// <summary>
    /// Body, serialized as JSON, for non GET query
    /// </summary>
    public object Body { get; set; }

    public WebHeaderCollection Headers
    {
        get
        {
            if (headers == null)
                headers = new WebHeaderCollection();
            return headers;
        }
    }
    WebHeaderCollection headers;

    public DecompressionMethods AutomaticDecompression { get; set; } = DecompressionMethods.None;

    public async Task<HttpWebResponse> QueryAsync()
    {
        var req = (HttpWebRequest)WebRequest.CreateHttp(url.ToString());
        req.Method = Method.Method;
        if (headers != null)
            req.Headers = Headers;
        if (cert != null)
            req.ClientCertificates.Add(cert);
        req.AutomaticDecompression = AutomaticDecompression;
        if (HttpMethod.Post.Equals(Method) || HttpMethod.Put.Equals(Method))
        {
            var tOs = new TaskCompletionSource<IAsyncResult>();
            req.BeginGetRequestStream(x => tOs.TrySetResult(x), null);
            await tOs.Task;
            using (var sOut = (Stream)req.EndGetRequestStream(tOs.Task.Result))
            {
                if (Body != null)
                {
                    using (var sw = new StreamWriter(sOut, Encoding.UTF8))
                    using (var jw = new JsonTextWriter(sw))
                    {
                        var ser = new JsonSerializer();
                        ser.Serialize(jw, Body);
                    }
                    req.ContentType = "application/json";
                }
            }
        }
        var tcs2 = new TaskCompletionSource<IAsyncResult>();
        req.BeginGetResponse(x => tcs2.TrySetResult(x), null);
        await tcs2.Task;
        var wr = (HttpWebResponse)req.EndGetResponse(tcs2.Task.Result);
        return wr;
    }

    public string Url { get { return url.ToString(); } }

    public async Task<T> QueryJsonAsync<T>()
    {
        var wr = await QueryAsync();
        ValidateResponse(wr);
        return ReadResponse<T>(wr);
    }

    public static void ValidateResponse(HttpWebResponse wr)
    {
        if (wr == null)
            throw new ArgumentNullException();
        bool success = (int)wr.StatusCode >= 200 && (int)wr.StatusCode <= 299;
        if (!success)
            throw new HttpRequestException($"Problem {wr.StatusCode}: {wr.StatusDescription}");
    }

    public static T ReadResponse<T>(HttpWebResponse wr)
    {
        var sResponse = wr.GetResponseStream();
        using (var sr = new StreamReader(wr.GetResponseStream()))
        using (var js = new JsonTextReader(sr))
        {
            var ser = new JsonSerializer();
            var result = ser.Deserialize<T>(js);
            return result;
        }
    }
    public static string ReadResponse(HttpWebResponse wr)
    {
        var sResponse = wr.GetResponseStream();
        using (var sr = new StreamReader(wr.GetResponseStream()))
            return sr.ReadToEnd();
    }
}
All in one Menu-Ribbon Bar
DirectX for WinRT/C# since 2013!
Taking over the world since 1371!

GeneralRe: We gonna get the client, finally! Pin
lmoelleb14-Jun-16 0:29
lmoelleb14-Jun-16 0:29 
GeneralRe: We gonna get the client, finally! Pin
Super Lloyd14-Jun-16 3:46
Super Lloyd14-Jun-16 3:46 
GeneralRe: We gonna get the client, finally! Pin
lmoelleb15-Jun-16 3:09
lmoelleb15-Jun-16 3:09 
GeneralRe: We gonna get the client, finally! Pin
Kenneth Haugland14-Jun-16 0:33
mvaKenneth Haugland14-Jun-16 0:33 
AnswerRe: We gonna get the client, finally! Pin
Super Lloyd14-Jun-16 4:11
Super Lloyd14-Jun-16 4:11 
GeneralEdmund Blackadder is alive and well... Pin
Mark_Wallace13-Jun-16 13:39
Mark_Wallace13-Jun-16 13:39 
GeneralRe: Edmund Blackadder is alive and well... Pin
Richard MacCutchan13-Jun-16 21:11
mveRichard MacCutchan13-Jun-16 21:11 
GeneralRe: Edmund Blackadder is alive and well... Pin
Richard Deeming14-Jun-16 1:11
mveRichard Deeming14-Jun-16 1:11 
GeneralRe: Edmund Blackadder is alive and well... Pin
Richard MacCutchan14-Jun-16 3:18
mveRichard MacCutchan14-Jun-16 3:18 
GeneralRe: Edmund Blackadder is alive and well... Pin
908236514-Jun-16 3:51
908236514-Jun-16 3:51 
GeneralRe: Edmund Blackadder is alive and well... Pin
Mark_Wallace14-Jun-16 7:01
Mark_Wallace14-Jun-16 7:01 
GeneralWhen coffee attack Pin
virang_2113-Jun-16 13:13
virang_2113-Jun-16 13:13 
GeneralRe: When coffee attack Pin
Mark_Wallace13-Jun-16 13:24
Mark_Wallace13-Jun-16 13:24 
GeneralRe: When coffee attack Pin
Ron Anders13-Jun-16 14:48
Ron Anders13-Jun-16 14:48 
QuestionRhetorical question about game engine Pin
Super Lloyd13-Jun-16 11:36
Super Lloyd13-Jun-16 11:36 
AnswerRe: Rhetorical question about game engine Pin
908236513-Jun-16 11:44
908236513-Jun-16 11:44 
GeneralRe: Rhetorical question about game engine Pin
Super Lloyd13-Jun-16 12:10
Super Lloyd13-Jun-16 12:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.