object ReadResponse(HttpClientMethod method, WebResponse response, Stream responseStream) { HttpWebResponse httpResponse = response as HttpWebResponse; if (httpResponse != null && (int)httpResponse.StatusCode >= 300) { throw new WebException(RequestResponseUtils.CreateResponseExceptionString(httpResponse, responseStream), null, WebExceptionStatus.ProtocolError, httpResponse); } if (method.readerType == null) { return(null); } // if (responseStream != null) { MimeReturnReader reader = (MimeReturnReader)MimeFormatter.CreateInstance(method.readerType, method.readerInitializer); return(reader.Read(response, responseStream)); } else { return(null); } }
object ReadResponse(HttpClientMethod method, WebResponse response, Stream responseStream) { HttpWebResponse httpResponse = response as HttpWebResponse; if (httpResponse != null && (int)httpResponse.StatusCode >= 300) { throw new WebException(RequestResponseUtils.CreateResponseExceptionString(httpResponse, responseStream), null, WebExceptionStatus.ProtocolError, httpResponse); } if (method.readerType == null) { return(null); } // CONSIDER,yannc: in future versions when we allow additional mime formatters we // : should consider giving them access to the response even if there is no // : response content. if (responseStream != null) { MimeReturnReader reader = (MimeReturnReader)MimeFormatter.CreateInstance(method.readerType, method.readerInitializer); return(reader.Read(response, responseStream)); } else { return(null); } }
MimeParameterWriter GetParameterWriter(HttpClientMethod method) { if (method.writerType == null) { return(null); } return((MimeParameterWriter)MimeFormatter.CreateInstance(method.writerType, method.writerInitializer)); }
private MimeParameterWriter GetParameterWriter(HttpClientMethod method) { if (method.writerType == null) { return null; } return (MimeParameterWriter) MimeFormatter.CreateInstance(method.writerType, method.writerInitializer); }
static void AddFormatter(Hashtable formatterTypes, Type formatterType, HttpClientMethod method) { if (formatterType == null) return; ArrayList list = (ArrayList)formatterTypes[formatterType]; if (list == null) { list = new ArrayList(); formatterTypes.Add(formatterType, list); } list.Add(method); }
protected void InvokeAsync(string methodName, string requestUrl, object[] parameters, SendOrPostCallback callback, object userState) { if (userState == null) { userState = base.NullToken; } AsyncOperation userAsyncState = AsyncOperationManager.CreateOperation(new UserToken(callback, userState)); WebClientAsyncResult result = new WebClientAsyncResult(this, null, null, new AsyncCallback(this.InvokeAsyncCallback), userAsyncState); try { base.AsyncInvokes.Add(userState, result); } catch (Exception exception) { if (((exception is ThreadAbortException) || (exception is StackOverflowException)) || (exception is OutOfMemoryException)) { throw; } if (Tracing.On) { Tracing.ExceptionCatch(TraceEventType.Error, this, "InvokeAsync", exception); } Exception exception2 = new ArgumentException(Res.GetString("AsyncDuplicateUserState"), exception); object[] results = new object[1]; InvokeCompletedEventArgs arg = new InvokeCompletedEventArgs(results, exception2, false, userState); userAsyncState.PostOperationCompleted(callback, arg); return; } try { HttpClientMethod clientMethod = this.GetClientMethod(methodName); MimeParameterWriter parameterWriter = this.GetParameterWriter(clientMethod); Uri requestUri = new Uri(requestUrl); if (parameterWriter != null) { parameterWriter.RequestEncoding = base.RequestEncoding; requestUrl = parameterWriter.GetRequestUrl(requestUri.AbsoluteUri, parameters); requestUri = new Uri(requestUrl, true); } result.InternalAsyncState = new InvokeAsyncState(clientMethod, parameterWriter, parameters); base.BeginSend(requestUri, result, parameterWriter.UsesWriteRequest); } catch (Exception exception3) { if (((exception3 is ThreadAbortException) || (exception3 is StackOverflowException)) || (exception3 is OutOfMemoryException)) { throw; } if (Tracing.On) { Tracing.ExceptionCatch(TraceEventType.Error, this, "InvokeAsync", exception3); } object[] objArray2 = new object[1]; base.OperationCompleted(userState, objArray2, exception3, false); } }
internal HttpClientType(Type type) { LogicalMethodInfo[] methodInfos = LogicalMethodInfo.Create(type.GetMethods(), LogicalMethodTypes.Sync); Hashtable formatterTypes = new Hashtable(); for (int i = 0; i < methodInfos.Length; i++) { LogicalMethodInfo methodInfo = methodInfos[i]; try { object[] attributes = methodInfo.GetCustomAttributes(typeof(HttpMethodAttribute)); if (attributes.Length == 0) { continue; } HttpMethodAttribute attribute = (HttpMethodAttribute)attributes[0]; HttpClientMethod method = new HttpClientMethod(); method.readerType = attribute.ReturnFormatter; method.writerType = attribute.ParameterFormatter; method.methodInfo = methodInfo; AddFormatter(formatterTypes, method.readerType, method); AddFormatter(formatterTypes, method.writerType, method); methods.Add(methodInfo.Name, method); } catch (Exception e) { if (e is ThreadAbortException || e is StackOverflowException || e is OutOfMemoryException) { throw; } throw new InvalidOperationException(Res.GetString(Res.WebReflectionError, methodInfo.DeclaringType.FullName, methodInfo.Name), e); } } foreach (Type t in formatterTypes.Keys) { ArrayList list = (ArrayList)formatterTypes[t]; LogicalMethodInfo[] m = new LogicalMethodInfo[list.Count]; for (int j = 0; j < list.Count; j++) { m[j] = ((HttpClientMethod)list[j]).methodInfo; } object[] initializers = MimeFormatter.GetInitializers(t, m); bool isWriter = typeof(MimeParameterWriter).IsAssignableFrom(t); for (int j = 0; j < list.Count; j++) { if (isWriter) { ((HttpClientMethod)list[j]).writerInitializer = initializers[j]; } else { ((HttpClientMethod)list[j]).readerInitializer = initializers[j]; } } } }
internal HttpClientType(Type type) { LogicalMethodInfo[] infoArray = LogicalMethodInfo.Create(type.GetMethods(), LogicalMethodTypes.Sync); Hashtable formatterTypes = new Hashtable(); for (int i = 0; i < infoArray.Length; i++) { LogicalMethodInfo info = infoArray[i]; try { object[] customAttributes = info.GetCustomAttributes(typeof(HttpMethodAttribute)); if (customAttributes.Length != 0) { HttpMethodAttribute attribute = (HttpMethodAttribute)customAttributes[0]; HttpClientMethod method = new HttpClientMethod { readerType = attribute.ReturnFormatter, writerType = attribute.ParameterFormatter, methodInfo = info }; AddFormatter(formatterTypes, method.readerType, method); AddFormatter(formatterTypes, method.writerType, method); this.methods.Add(info.Name, method); } } catch (Exception exception) { if (((exception is ThreadAbortException) || (exception is StackOverflowException)) || (exception is OutOfMemoryException)) { throw; } throw new InvalidOperationException(Res.GetString("WebReflectionError", new object[] { info.DeclaringType.FullName, info.Name }), exception); } } foreach (Type type2 in formatterTypes.Keys) { ArrayList list = (ArrayList)formatterTypes[type2]; LogicalMethodInfo[] methodInfos = new LogicalMethodInfo[list.Count]; for (int j = 0; j < list.Count; j++) { methodInfos[j] = ((HttpClientMethod)list[j]).methodInfo; } object[] initializers = MimeFormatter.GetInitializers(type2, methodInfos); bool flag = typeof(MimeParameterWriter).IsAssignableFrom(type2); for (int k = 0; k < list.Count; k++) { if (flag) { ((HttpClientMethod)list[k]).writerInitializer = initializers[k]; } else { ((HttpClientMethod)list[k]).readerInitializer = initializers[k]; } } } }
HttpClientMethod GetClientMethod(string methodName) { HttpClientMethod method = clientType.GetMethod(methodName); if (method == null) { throw new ArgumentException(Res.GetString(Res.WebInvalidMethodName, methodName), "methodName"); } return(method); }
private HttpClientMethod GetClientMethod(string methodName) { HttpClientMethod method = this.clientType.GetMethod(methodName); if (method == null) { throw new ArgumentException(Res.GetString("WebInvalidMethodName", new object[] { methodName }), "methodName"); } return(method); }
internal HttpClientType(Type type) { LogicalMethodInfo[] infoArray = LogicalMethodInfo.Create(type.GetMethods(), LogicalMethodTypes.Sync); Hashtable formatterTypes = new Hashtable(); for (int i = 0; i < infoArray.Length; i++) { LogicalMethodInfo info = infoArray[i]; try { object[] customAttributes = info.GetCustomAttributes(typeof(HttpMethodAttribute)); if (customAttributes.Length != 0) { HttpMethodAttribute attribute = (HttpMethodAttribute) customAttributes[0]; HttpClientMethod method = new HttpClientMethod { readerType = attribute.ReturnFormatter, writerType = attribute.ParameterFormatter, methodInfo = info }; AddFormatter(formatterTypes, method.readerType, method); AddFormatter(formatterTypes, method.writerType, method); this.methods.Add(info.Name, method); } } catch (Exception exception) { if (((exception is ThreadAbortException) || (exception is StackOverflowException)) || (exception is OutOfMemoryException)) { throw; } throw new InvalidOperationException(Res.GetString("WebReflectionError", new object[] { info.DeclaringType.FullName, info.Name }), exception); } } foreach (Type type2 in formatterTypes.Keys) { ArrayList list = (ArrayList) formatterTypes[type2]; LogicalMethodInfo[] methodInfos = new LogicalMethodInfo[list.Count]; for (int j = 0; j < list.Count; j++) { methodInfos[j] = ((HttpClientMethod) list[j]).methodInfo; } object[] initializers = MimeFormatter.GetInitializers(type2, methodInfos); bool flag = typeof(MimeParameterWriter).IsAssignableFrom(type2); for (int k = 0; k < list.Count; k++) { if (flag) { ((HttpClientMethod) list[k]).writerInitializer = initializers[k]; } else { ((HttpClientMethod) list[k]).readerInitializer = initializers[k]; } } } }
/// <include file='doc\HttpClientProtocol.uex' path='docs/doc[@for="HttpSimpleClientProtocol.InvokeAsync1"]/*' /> /// <devdoc> /// <para>[To be supplied.]</para> /// </devdoc> protected void InvokeAsync(string methodName, string requestUrl, object[] parameters, SendOrPostCallback callback, object userState) { if (userState == null) { userState = NullToken; } AsyncOperation asyncOp = AsyncOperationManager.CreateOperation(new UserToken(callback, userState)); WebClientAsyncResult asyncResult = new WebClientAsyncResult(this, null, null, new AsyncCallback(InvokeAsyncCallback), asyncOp); try { AsyncInvokes.Add(userState, asyncResult); } catch (Exception e) { if (e is ThreadAbortException || e is StackOverflowException || e is OutOfMemoryException) { throw; } if (Tracing.On) { Tracing.ExceptionCatch(TraceEventType.Error, this, "InvokeAsync", e); } Exception exception = new ArgumentException(Res.GetString(Res.AsyncDuplicateUserState), e); InvokeCompletedEventArgs eventArgs = new InvokeCompletedEventArgs(new object[] { null }, exception, false, userState); asyncOp.PostOperationCompleted(callback, eventArgs); return; } try { HttpClientMethod method = GetClientMethod(methodName); MimeParameterWriter paramWriter = GetParameterWriter(method); Uri requestUri = new Uri(requestUrl); if (paramWriter != null) { paramWriter.RequestEncoding = RequestEncoding; requestUrl = paramWriter.GetRequestUrl(requestUri.AbsoluteUri, parameters); requestUri = new Uri(requestUrl, true); } asyncResult.InternalAsyncState = new InvokeAsyncState(method, paramWriter, parameters); BeginSend(requestUri, asyncResult, paramWriter.UsesWriteRequest); } catch (Exception e) { if (e is ThreadAbortException || e is StackOverflowException || e is OutOfMemoryException) { throw; } if (Tracing.On) { Tracing.ExceptionCatch(TraceEventType.Error, this, "InvokeAsync", e); } OperationCompleted(userState, new object[] { null }, e, false); } }
private object ReadResponse(HttpClientMethod method, WebResponse response, Stream responseStream) { HttpWebResponse response2 = response as HttpWebResponse; if ((response2 != null) && (response2.StatusCode >= HttpStatusCode.MultipleChoices)) { throw new WebException(RequestResponseUtils.CreateResponseExceptionString(response2, responseStream), null, WebExceptionStatus.ProtocolError, response2); } if ((method.readerType != null) && (responseStream != null)) { MimeReturnReader reader = (MimeReturnReader)MimeFormatter.CreateInstance(method.readerType, method.readerInitializer); return(reader.Read(response, responseStream)); } return(null); }
static void AddFormatter(Hashtable formatterTypes, Type formatterType, HttpClientMethod method) { if (formatterType == null) { return; } ArrayList list = (ArrayList)formatterTypes[formatterType]; if (list == null) { list = new ArrayList(); formatterTypes.Add(formatterType, list); } list.Add(method); }
/// <include file='doc\HttpClientProtocol.uex' path='docs/doc[@for="HttpSimpleClientProtocol.BeginInvoke"]/*' /> /// <devdoc> /// <para> /// Starts an asynchronous invocation of a method of a HTTP web service. /// </para> /// </devdoc> protected IAsyncResult BeginInvoke(string methodName, string requestUrl, object[] parameters, AsyncCallback callback, object asyncState) { HttpClientMethod method = GetClientMethod(methodName); MimeParameterWriter paramWriter = GetParameterWriter(method); Uri requestUri = new Uri(requestUrl); if (paramWriter != null) { paramWriter.RequestEncoding = RequestEncoding; requestUrl = paramWriter.GetRequestUrl(requestUri.AbsoluteUri, parameters); requestUri = new Uri(requestUrl, true); } InvokeAsyncState invokeState = new InvokeAsyncState(method, paramWriter, parameters); return(BeginSend(requestUri, invokeState, callback, asyncState, paramWriter.UsesWriteRequest)); }
protected IAsyncResult BeginInvoke(string methodName, string requestUrl, object[] parameters, AsyncCallback callback, object asyncState) { HttpClientMethod clientMethod = this.GetClientMethod(methodName); MimeParameterWriter parameterWriter = this.GetParameterWriter(clientMethod); Uri requestUri = new Uri(requestUrl); if (parameterWriter != null) { parameterWriter.RequestEncoding = base.RequestEncoding; requestUrl = parameterWriter.GetRequestUrl(requestUri.AbsoluteUri, parameters); requestUri = new Uri(requestUrl, true); } InvokeAsyncState internalAsyncState = new InvokeAsyncState(clientMethod, parameterWriter, parameters); WebClientAsyncResult asyncResult = new WebClientAsyncResult(this, internalAsyncState, null, callback, asyncState); return(base.BeginSend(requestUri, asyncResult, parameterWriter.UsesWriteRequest)); }
internal HttpClientType(Type type) { LogicalMethodInfo[] methodInfos = LogicalMethodInfo.Create(type.GetMethods(), LogicalMethodTypes.Sync); Hashtable formatterTypes = new Hashtable(); for (int i = 0; i < methodInfos.Length; i++) { LogicalMethodInfo methodInfo = methodInfos[i]; try { object[] attributes = methodInfo.GetCustomAttributes(typeof(HttpMethodAttribute)); if (attributes.Length == 0) continue; HttpMethodAttribute attribute = (HttpMethodAttribute)attributes[0]; HttpClientMethod method = new HttpClientMethod(); method.readerType = attribute.ReturnFormatter; method.writerType = attribute.ParameterFormatter; method.methodInfo = methodInfo; AddFormatter(formatterTypes, method.readerType, method); AddFormatter(formatterTypes, method.writerType, method); methods.Add(methodInfo.Name, method); } catch (Exception e) { if (e is ThreadAbortException || e is StackOverflowException || e is OutOfMemoryException) { throw; } throw new InvalidOperationException(Res.GetString(Res.WebReflectionError, methodInfo.DeclaringType.FullName, methodInfo.Name), e); } } foreach (Type t in formatterTypes.Keys) { ArrayList list = (ArrayList)formatterTypes[t]; LogicalMethodInfo[] m = new LogicalMethodInfo[list.Count]; for (int j = 0; j < list.Count; j++) m[j] = ((HttpClientMethod)list[j]).methodInfo; object[] initializers = MimeFormatter.GetInitializers(t, m); bool isWriter = typeof(MimeParameterWriter).IsAssignableFrom(t); for (int j = 0; j < list.Count; j++) { if (isWriter) { ((HttpClientMethod)list[j]).writerInitializer = initializers[j]; } else { ((HttpClientMethod)list[j]).readerInitializer = initializers[j]; } } } }
internal InvokeAsyncState(HttpClientMethod method, MimeParameterWriter paramWriter, object[] parameters) { this.Method = method; this.ParamWriter = paramWriter; this.Parameters = parameters; }
private object ReadResponse(HttpClientMethod method, WebResponse response, Stream responseStream) { HttpWebResponse response2 = response as HttpWebResponse; if ((response2 != null) && (response2.StatusCode >= HttpStatusCode.MultipleChoices)) { throw new WebException(RequestResponseUtils.CreateResponseExceptionString(response2, responseStream), null, WebExceptionStatus.ProtocolError, response2); } if ((method.readerType != null) && (responseStream != null)) { MimeReturnReader reader = (MimeReturnReader) MimeFormatter.CreateInstance(method.readerType, method.readerInitializer); return reader.Read(response, responseStream); } return null; }
object ReadResponse(HttpClientMethod method, WebResponse response, Stream responseStream) { HttpWebResponse httpResponse = response as HttpWebResponse; if (httpResponse != null && (int)httpResponse.StatusCode >= 300) throw new WebException(RequestResponseUtils.CreateResponseExceptionString(httpResponse, responseStream), null, WebExceptionStatus.ProtocolError, httpResponse); if (method.readerType == null) return null; // if (responseStream != null) { MimeReturnReader reader = (MimeReturnReader)MimeFormatter.CreateInstance(method.readerType, method.readerInitializer); return reader.Read(response, responseStream); } else return null; }
/// <include file='doc\HttpClientProtocol.uex' path='docs/doc[@for="HttpSimpleClientProtocol.Invoke"]/*' /> /// <devdoc> /// <para> /// Invokes a method of a HTTP web service. /// </para> /// </devdoc> protected object Invoke(string methodName, string requestUrl, object[] parameters) { WebResponse response = null; HttpClientMethod method = GetClientMethod(methodName); MimeParameterWriter paramWriter = GetParameterWriter(method); Uri requestUri = new Uri(requestUrl); if (paramWriter != null) { paramWriter.RequestEncoding = RequestEncoding; requestUrl = paramWriter.GetRequestUrl(requestUri.AbsoluteUri, parameters); requestUri = new Uri(requestUrl, true); } WebRequest request = null; try { request = GetWebRequest(requestUri); NotifyClientCallOut(request); PendingSyncRequest = request; if (paramWriter != null) { paramWriter.InitializeRequest(request, parameters); // if (paramWriter.UsesWriteRequest) { if (parameters.Length == 0) { request.ContentLength = 0; } else { Stream requestStream = null; try { requestStream = request.GetRequestStream(); paramWriter.WriteRequest(requestStream, parameters); } finally { if (requestStream != null) { requestStream.Close(); } } } } } response = GetWebResponse(request); Stream responseStream = null; if (response.ContentLength != 0) { responseStream = response.GetResponseStream(); } return(ReadResponse(method, response, responseStream)); } finally { if (request == PendingSyncRequest) { PendingSyncRequest = null; } } }
protected object Invoke(string methodName, string requestUrl, object[] parameters) { WebResponse webResponse = null; object obj2; HttpClientMethod clientMethod = this.GetClientMethod(methodName); MimeParameterWriter parameterWriter = this.GetParameterWriter(clientMethod); Uri uri = new Uri(requestUrl); if (parameterWriter != null) { parameterWriter.RequestEncoding = base.RequestEncoding; requestUrl = parameterWriter.GetRequestUrl(uri.AbsoluteUri, parameters); uri = new Uri(requestUrl, true); } WebRequest webRequest = null; try { webRequest = this.GetWebRequest(uri); base.NotifyClientCallOut(webRequest); base.PendingSyncRequest = webRequest; if (parameterWriter != null) { parameterWriter.InitializeRequest(webRequest, parameters); if (parameterWriter.UsesWriteRequest) { if (parameters.Length == 0) { webRequest.ContentLength = 0L; } else { Stream requestStream = null; try { requestStream = webRequest.GetRequestStream(); parameterWriter.WriteRequest(requestStream, parameters); } finally { if (requestStream != null) { requestStream.Close(); } } } } } webResponse = this.GetWebResponse(webRequest); Stream responseStream = null; if (webResponse.ContentLength != 0L) { responseStream = webResponse.GetResponseStream(); } obj2 = this.ReadResponse(clientMethod, webResponse, responseStream); } finally { if (webRequest == base.PendingSyncRequest) { base.PendingSyncRequest = null; } } return(obj2); }
/// <include file='doc\HttpClientProtocol.uex' path='docs/doc[@for="HttpSimpleClientProtocol.Invoke"]/*' /> /// <devdoc> /// <para> /// Invokes a method of a HTTP web service. /// </para> /// </devdoc> protected object Invoke(string methodName, string requestUrl, object[] parameters) { WebResponse response = null; HttpClientMethod method = GetClientMethod(methodName); MimeParameterWriter paramWriter = GetParameterWriter(method); Uri requestUri = new Uri(requestUrl); if (paramWriter != null) { paramWriter.RequestEncoding = RequestEncoding; requestUrl = paramWriter.GetRequestUrl(requestUri.AbsoluteUri, parameters); requestUri = new Uri(requestUrl, true); } WebRequest request = null; try { request = GetWebRequest(requestUri); PendingSyncRequest = request; if (paramWriter != null) { paramWriter.InitializeRequest(request, parameters); // CONSIDER,yannc: in future versions when we allow pluggable protocols // we may want to let them write in the request stream even // if there are no parameters. if (paramWriter.UsesWriteRequest) { if (parameters.Length == 0) { request.ContentLength = 0; } else { Stream requestStream = null; try { requestStream = request.GetRequestStream(); paramWriter.WriteRequest(requestStream, parameters); } finally { if (requestStream != null) { requestStream.Close(); } } } } } response = GetWebResponse(request); Stream responseStream = null; if (response.ContentLength != 0) { responseStream = response.GetResponseStream(); } return(ReadResponse(method, response, responseStream)); } finally { if (request == PendingSyncRequest) { PendingSyncRequest = null; } } }