public virtual Task <XboxLiveHttpResponse> GetResponseWithoutAuth(HttpCallResponseBodyType httpCallResponseBodyType) { if (!string.IsNullOrEmpty(this.ContractVersion)) { this.SetCustomHeader("x-xbl-contract-version", this.ContractVersion); } foreach (KeyValuePair <string, string> customHeader in this.customHeaders) { this.webRequest.Headers[customHeader.Key] = customHeader.Value; } TaskCompletionSource <XboxLiveHttpResponse> getResponseCompletionSource = new TaskCompletionSource <XboxLiveHttpResponse>(); this.WriteRequestBodyAsync().ContinueWith(writeBodyTask => { // The explicit cast in the next method should not be necessary, but Visual Studio is complaining // that the call is ambiguous. This removes that in-editor error. Task.Factory.FromAsync(this.webRequest.BeginGetResponse, (Func <IAsyncResult, WebResponse>) this.webRequest.EndGetResponse, null) .ContinueWith(getResponseTask => { if (getResponseTask.IsFaulted) { getResponseCompletionSource.SetException(getResponseTask.Exception); return; } var response = new XboxLiveHttpResponse((HttpWebResponse)getResponseTask.Result, httpCallResponseBodyType); getResponseCompletionSource.SetResult(response); }); }); return(getResponseCompletionSource.Task); }
internal XboxLiveHttpResponse(HttpWebResponse response, HttpCallResponseBodyType bodyType) { this.response = response; using (Stream body = response.GetResponseStream()) { this.Initialize((int)response.StatusCode, body, bodyType, response.ContentLength, "utf-8", response.Headers); } }
protected void Initialize(int httpStatus, Stream body, HttpCallResponseBodyType bodyType, long contentLength, string characterSet, WebHeaderCollection headers) { this.HttpStatus = httpStatus; this.BodyType = bodyType; this.Headers = new Dictionary <string, string>(); int vectorSize = contentLength > int.MaxValue ? int.MaxValue : (int)contentLength; this.ResponseBodyVector = new byte[vectorSize]; if (contentLength > 0) { int totalBytesRead = 0; do { int bytesRead = body.Read(this.ResponseBodyVector, totalBytesRead, this.ResponseBodyVector.Length - totalBytesRead); // This means we're at the end of the stream. if (bytesRead == 0) { throw new ArgumentException(string.Format("Expected body stream to contain {0} bytes but only read {1} bytes.", contentLength, totalBytesRead), "body"); } totalBytesRead += bytesRead; }while (totalBytesRead < contentLength); Encoding encoding; switch (characterSet.ToLower()) { case "utf-8": encoding = Encoding.UTF8; break; case "ascii": encoding = Encoding.ASCII; break; default: encoding = Encoding.UTF8; break; } using (MemoryStream ms = new MemoryStream(this.ResponseBodyVector)) { using (StreamReader sr = new StreamReader(ms, encoding)) { this.ResponseBodyString = sr.ReadToEnd(); this.ResponseBodyJson = this.ResponseBodyString; } } } for (int i = 0; i < headers.Count; ++i) { var key = headers.AllKeys[i]; this.Headers.Add(key, headers[key]); } }
public override Task <XboxLiveHttpResponse> GetResponseWithoutAuth(HttpCallResponseBodyType httpCallResponseBodyType) { // Save the mock data out for testing. string requestData = JsonConvert.SerializeObject(this, Formatting.Indented); string outputDir = @"C:\Temp\MockData"; Directory.CreateDirectory(outputDir); string outputPath = Path.Combine(outputDir, "data.txt"); File.AppendAllText(outputPath, requestData); return(Task.FromResult(MockXboxLiveData.GetMockResponse(this))); }
public MockXboxLiveHttpResponse(int httpStatus, JObject body, HttpCallResponseBodyType bodyType, string characterSet = null, Dictionary <string, string> headers = null) { string bodyJson = JsonConvert.SerializeObject(body); byte[] bodyBytes = Encoding.UTF8.GetBytes(bodyJson ?? ""); Stream bodyStream = new MemoryStream(bodyBytes); WebHeaderCollection webHeaders = new WebHeaderCollection(); if (headers != null) { foreach (KeyValuePair <string, string> header in headers) { webHeaders[header.Key] = header.Value; } } this.Initialize(httpStatus, bodyStream, bodyType, bodyStream.Length, characterSet ?? "utf-8", webHeaders); }
public Task <XboxLiveHttpResponse> GetResponseWithAuth(XboxLiveUser user, HttpCallResponseBodyType httpCallResponseBodyType) { TaskCompletionSource <XboxLiveHttpResponse> getResponseCompletionSource = new TaskCompletionSource <XboxLiveHttpResponse>(); user.GetTokenAndSignatureAsync(this.Method, this.Url, this.Headers).ContinueWith( tokenTask => { if (tokenTask.IsFaulted) { getResponseCompletionSource.SetException(tokenTask.Exception); return; } var result = tokenTask.Result; #if !WINDOWS_UWP this.SetCustomHeader(AuthorizationHeaderName, string.Format("XBL3.0 x={0};{1}", result.XboxUserHash, result.Token)); #else this.SetCustomHeader(AuthorizationHeaderName, string.Format("{0}", result.Token)); #endif this.SetCustomHeader(SignatureHeaderName, tokenTask.Result.Signature); try { this.GetResponseWithoutAuth(httpCallResponseBodyType).ContinueWith(getResponseTask => { if (getResponseTask.IsFaulted) { getResponseCompletionSource.SetException(getResponseTask.Exception); } getResponseCompletionSource.SetResult(getResponseTask.Result); }); } catch (Exception e) { getResponseCompletionSource.SetException(e); } }); return(getResponseCompletionSource.Task); }
internal XboxLiveHttpResponse( int httpStatusCode, bool networkFailure, HttpWebResponse response, DateTime responseReceivedTime, DateTime requestStartTime, string xboxUserId, XboxLiveSettings contextSettings, string url, XboxLiveAPIName xboxLiveAPI, string method, string requestBody, HttpCallResponseBodyType responseBodyType ) { this.HttpStatus = httpStatusCode; this.NetworkFailure = networkFailure; this.response = response; this.ResponseReceivedTime = responseReceivedTime; this.RequestStartTime = requestStartTime; this.XboxUserId = xboxUserId; this.ContextSettings = contextSettings; this.Url = url; this.XboxLiveAPI = xboxLiveAPI; this.Method = method; this.RequestBody = requestBody; this.ResponseBodyType = responseBodyType; if (response != null) { using (Stream body = response.GetResponseStream()) { this.Initialize((int)response.StatusCode, body, response.ContentLength, "utf-8", response.Headers); } } }