0

I wrote some code:

this.req = (HttpWebRequest)WebRequest.Create(this._urlNBKIMain);
this.req.Accept = this._accept;
this.req.Headers.Add(this._acceptLanguage);
this.req.UserAgent = this._userAgent;
this.req.Host = this._hostNBKI;
this.req.KeepAlive = this._keepAlive;
this.req.AutomaticDecompression = (DecompressionMethods.GZip | DecompressionMethods.Deflate);
this.req.CookieContainer = this.cookieContainer;
ServicePoint servicePoint = this.req.ServicePoint;
PropertyInfo property = servicePoint.GetType().GetProperty("HttpBehaviour", BindingFlags.Instance | BindingFlags.NonPublic);
property.SetValue(servicePoint, 0, null);
this.res = (HttpWebResponse)this.req.GetResponse();

But, I got runtime error:

System.ArgumentException: It is not possible to convert an object of type "System.Int32" to type "System.Net.HttpBehaviour".
   in System.RuntimeType.TryChangeType(Object value, Binder binder, CultureInfo culture, Boolean needsSpecialCast)
   in System.RuntimeType.CheckValue(Object value, Binder binder, CultureInfo culture, BindingFlags invokeAttr)
   in System.Reflection.MethodBase.CheckArguments(Object[] parameters, Binder binder, BindingFlags invokeAttr, CultureInfo culture, Signature sig)
   in System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
   in System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   in System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture)
   in System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, Object[] index)
   in CheckNBKI.GetMainFormNBKI()

Help me, please.

krlzlx
  • 5,752
  • 14
  • 47
  • 55
Bibo325
  • 23
  • 3
  • In what line exactly this exception is being thrown? – Ricardo Silva Apr 30 '15 at 13:19
  • @RicardoSilva I imagine it's this line: `property.SetValue(servicePoint, 0, null);` – spender Apr 30 '15 at 13:21
  • Are you trying to run this directly into IIS? If so, try put the pdb file with the compiled assembly. The pdb file will provide you a more detailed exception (including the line where exception is thrown) – Ricardo Silva Apr 30 '15 at 13:23

1 Answers1

1

You are trying to set an enum to an integer value. You should use the enum instead. Since the enum isn't available (it is an internal of the .NET Framework), you could use some tricks to get an instance of the enum value:

object val = Convert.ChangeType(0, Enum.GetUnderlyingType(property.PropertyType));

property.SetValue(servicePoint, val, null);

I have had some help from here.

Community
  • 1
  • 1
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325