/*--------------------------------------------------------------------------------------- * Response from data.gov.au is a JSON string. If successful, it will look something like: * { * "help": "...", * "success"": true, * "result": { * "resource_group_id" : "...", * "cache_last_updated" : null, * "datastore_active": false, * ... * } * } * If unsuccessful, the response will look something like: * { * "help": "...", * "success"": false, * "error": { * ""__type" : "Validation Error" * } * } *---------------------------------------------------------------------------------------*/ public static DataGovOutcome InterpretResponse(string postStatus) { Dictionary <string, Object> ResponseFields = Deserialise(postStatus); DataGovOutcome Outcome = new DataGovOutcome(); Outcome.PostStatus = postStatus; string OutcomeStatusMessage = ExtractOutcome(ResponseFields); string OutcomeStatusBody = ExtractResponseValues(ResponseFields); Outcome.Success = !(String.IsNullOrEmpty(OutcomeStatusMessage)); Outcome.FormattedMessage = OutcomeStatusMessage + "\r\n" + OutcomeStatusBody; return(Outcome); }
//--------------------------------------------------------------------------------------- void SubmitButton_Click(Object sender, EventArgs e) { try { DataGovOutcome Outcome = DataGovManager.UploadToDataGov(SetParameters()); TextBoxOutcome.Text = Outcome.FormattedMessage; this.SuccessPlace.Visible = true; this.ErrorPlace.Visible = false; ; } catch (Exception ex) { TextBoxOutcome.Text = ex.ToString(); this.ErrorPlace.Visible = true; this.SuccessPlace.Visible = false; } }
/*--------------------------------------------------------------------------------------- * Upload a new version of the document as a multipart/form-data * ---------------------------------------------------------------------------------------*/ private static DataGovOutcome Postfile(DataGovParameters parameters) { // Boundary is a unique string used to deliminate form values sent in the request string Boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x"); byte[] BoundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + Boundary + "\r\n"); HttpWebRequest PostRequest = null; Stream PostStream = null; DataGovOutcome Outcome = new DataGovOutcome(); try { PostRequest = ConfigureUploadRequest(parameters, Boundary); PostStream = PostRequest.GetRequestStream(); PostStandardFormHeader(PostStream, BoundaryBytes, parameters); PostFilenameHeader(PostStream, BoundaryBytes, parameters.Filename); PostFileContentsData(PostStream, parameters.Filename); PostTrailerData(PostStream, Boundary); Outcome.PostStatus = ReadPostResponse(PostRequest); Outcome.Success = true; } // The web response may contain more information about the exception catch (WebException ex) { if (ex.Response != null) { using (Stream ErrorStream = ex.Response.GetResponseStream()) using (StreamReader reader = new StreamReader(ErrorStream)) { Outcome.PostStatus = reader.ReadToEnd(); Outcome.Success = false; return(Outcome); } } else { throw; } } catch { throw; } finally { if (PostStream != null) { PostStream.Close(); PostRequest = null; } } return(Outcome); }
//--------------------------------------------------------------------------------------- public static DataGovOutcome UploadToDataGov(DataGovParameters parameters) { DataGovOutcome Outcome = null; try { Outcome = Postfile(parameters); if (Outcome.Success) { return(ResponseInterpreter.InterpretResponse(Outcome.PostStatus)); } else { return(Outcome); } } catch { throw; } }