使用Apache的commons-httpclient for Java,将查询参数添加到GetMethod实例的最佳方法是什么?如果我使用PostMethod,它非常简单:
PostMethod method = new PostMethod();
method.addParameter("key", "value");
Run Code Online (Sandbox Code Playgroud)
但是,GetMethod没有"addParameter"方法.我发现这有效:
GetMethod method = new GetMethod("http://www.example.com/page");
method.setQueryString(new NameValuePair[] {
new NameValuePair("key", "value")
});
Run Code Online (Sandbox Code Playgroud)
但是,我见过的大多数示例都是将参数直接硬编码到URL中,例如:
GetMethod method = new GetMethod("http://www.example.com/page?key=value");
Run Code Online (Sandbox Code Playgroud)
或者对查询字符串进行硬编码,例如:
GetMethod method = new GetMethod("http://www.example.com/page");
method.setQueryString("?key=value");
Run Code Online (Sandbox Code Playgroud)
这些模式中的一种是首选吗?为什么PostMethod和GetMethod之间的API差异?那些用于其他HttpMethodParams方法的是什么?
我Type.GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)
用来检索给定类型的方法数组.
问题是返回的MethodInfo
可能包括由我不想要的编译器生成的方法.例如:
财产bool Enabled { get; }
将获得bool get_Enabled()
事件SomethingChanged
会得到
add_SomethingChanged(EventHandler)
和
remove_SomethingChanged(EventHandler)
我可以添加一些过滤器逻辑来摆脱它们,这可能会变得非常复杂.我想知道是否还有其他我可以做的事情,例如BindingFlags
设置,只检索用户定义的方法?
我有以下类树:
public class A
{
public static object GetMe(SomeOtherClass something)
{
return something.Foo();
}
}
public class B:A
{
public static new object GetMe(SomeOtherClass something)
{
return something.Bar();
}
}
public class C:B
{
}
public class SomeOtherClass
{
}
Run Code Online (Sandbox Code Playgroud)
鉴于SomeOtherClass parameter = new SomeOtherClass()
)这工作:
typeof(B).GetMethod("GetMe", new Type[] { typeof(SomeOtherClass) })).Invoke(null, parameter));
Run Code Online (Sandbox Code Playgroud)
但是这个:
typeof(C).GetMethod("GetMe", new Type[] { typeof(SomeOtherClass) })).Invoke(null, parameter));
Run Code Online (Sandbox Code Playgroud)
抛出一个NullReferenceException
,虽然我希望它会调用与上面完全相同的方法.
我试过几个绑定标志无济于事.有帮助吗?
我正在使用.NET兼容的编译器Reflection.Emit
.问题是,虽然TypeBuilder
是派生自Type
,但它不允许我使用Type提供的所有方便的方法.
我真正关心的问题是:
有没有办法获得a中定义的方法,字段,属性,构造函数等的列表TypeBuilder
,或者我真的需要TypeBuilderWrapper
自己跟踪所有这些实体?它必须将它们存储在内部,所以必须有一些方法来提取它们?
该GetMethod
方法非常方便,因为它可以找到最佳拟合方法覆盖考虑继承和通用协方差.我真的必须自己重新实现TypeBuilderWrapper
吗?
同样的问题也可能适用于MethodBuilder
,FieldBuilder
等它,我相信,没有实现的查找方法MethodInfo
,并FieldInfo
分别.
对于我正在创建的真正抽象的应用程序,我需要在不知道其参数类型的情况下调用方法,并且只知道String形状中的参数.
让我说我有方法;
getNames(String like, int amount);
Run Code Online (Sandbox Code Playgroud)
我有一个包含2个参数的字符串数组,所以我说我有;
String[] params = new String[] {"jack", "25"};
Run Code Online (Sandbox Code Playgroud)
有什么方法可以使用params数组获取并调用此方法吗?
我有一个bean,我想通过反射访问它的属性.我以字符串形式接收属性名称.bean具有私有字段的getter方法.
我目前正在使用该字段getDeclaredField(fieldName)
,通过使用setAccessible(true)
然后使用它来检索其值来访问它get
.
另一种方法是将字段名称大写并添加get
到它的前面,然后从类中获取该名称的方法,最后调用该方法以获取私有字段的值.
哪种方式更好?
编辑
也许我应该用"更好"来解释我的意思......通过"更好",我的意思是在最佳实践意义上.或者,如果有任何微妙的警告或差异.
我试过一个小程序...我想运行一个程序,看看c#class中的所有方法名称......这是代码
class Program
{
public int adf()
{
return 0;
}
static void Main(string[] args)
{
foreach (MethodInfo mInfo in typeof(Program).GetMethods(BindingFlags.NonPublic | BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static))
{
Console.WriteLine(mInfo.Name);
}
Console.ReadKey();
}
private void bdf()
{
Console.WriteLine("Dg");
}
}
Run Code Online (Sandbox Code Playgroud)
它的工作正常,结果我得到了这个
adf
main
bdf
Run Code Online (Sandbox Code Playgroud)
现在,我想传递给GetMethods函数只有一个参数并得到结果......我认为这不是用'二进制或(|)'传递5个参数的好方法...在BindingFlags Enum是19个字段和什么如果我想传递18个参数xD 我怎么能只传递一个值呢?
这是Enum
public enum BindingFlags
{
Default = 0,
IgnoreCase = 1,
DeclaredOnly = 2,
Instance = 4,
Static = 8,
Public = 16,
NonPublic = 32,
FlattenHierarchy = 64,
InvokeMethod = 256, …
Run Code Online (Sandbox Code Playgroud) 这是我的帖子详细视图,效果非常好。
class PostDetailView(DetailView):
model = Post
context_object_name = 'post'
template_name = 'posts/detail.html'
def get_queryset(self, *args, **kwargs):
request = self.request
pk = self.kwargs.get('pk')
queryset = Post.objects.filter(pk=pk)
return queryset
def get_context_data(self, **kwargs):
context = super(PostDetailView, self).get_context_data(**kwargs)
content['comments'] = Comment.objects.all()
return context
Run Code Online (Sandbox Code Playgroud)
但是,当我将 get 方法添加到视图时,它不再起作用。
def get(self, request, *args, **kwargs):
# how to return here so that it works exactly like before
Run Code Online (Sandbox Code Playgroud)
添加 get 方法后get_queryset
,get_context_data
不会自动调用,并且模板中的上下文为空。那么 get 方法应该怎样才能让它像以前一样工作呢?
编辑 我的目标是做这样的事情
if request.is_ajax():
html = render_to_string('comments/detail.html') # ajax reply with html …
Run Code Online (Sandbox Code Playgroud) 我正在使用反射创建一个对象的实例,并在对象的类中获取方法,但是当我必须使用类型数组Type
来避免歧义问题时,问题就来了,这里是我的代码示例我试图达到.
public class BigClass
{
public void getSomething(XmlDocument doc, ref CustomObject obj) {...}
public void getSomething(XmlDocument doc, ref CustomObject obj, string id) {...}
}
Run Code Online (Sandbox Code Playgroud)
此代码来自外部程序集(file.dll),我正在使用下一个代码.
Assembly a = Assembly.LoadFrom("file.dll");
Type s = a.GetType("FileNamespace.BigClass");
MethodInfo inf = s.GetMethod("getSomething", new [] {typeof(XmlDocument), typeof(CustomObject), typeof(string)});
Run Code Online (Sandbox Code Playgroud)
要获取MethodInfo
使用3个参数的对象,但变量"inf"为空,我认为因为它没有找到使用"ref"的参数的方法.
有办法解决这个问题吗?
我知道symfony api解释说getMethod()获取请求“意图”方法,而getRealMethod()获取“真实”请求方法,但我不知道“意图”和“真实”是什么意思。谁能告诉我?谢谢
我正在使用 BasicHTTPClient 通过 GET 方法将数据从 esp32 cam board 发送到网络服务器 我已经在 google 上搜索了这个错误 可以解释一下这个错误并告诉我如何解决它
先感谢您!
这是代码,但它不适用于IE8和7(IE9,chrome,firefox,safari,opera都可以).我尝试了很多东西(meta utf-8代码,php头代码,接收警报,缓存:false).我能做什么,我需要帮助.谢谢你的兴趣.
var request = $.ajax({
type:"GET",
url: "_veri.php?t=icerik_getir&id="+tabopen,
dataType: "html",
});
request.done(function(msg) {
$(".tab-contentmenu").html(msg);
});
Run Code Online (Sandbox Code Playgroud)
编辑:
alert为我提供了所有浏览器中请求的数据,但仍然没有".tab-contentmenu"中的请求数据,我该怎么办?
var request = $.ajax({
type:"GET",
context: document.body,
url: "_veri.php?t=icerik_getir&id="+tabopen,
dataType: "html"
});
request.done(function(msg) {
$(".tab-contentmenu").html(msg);
alert(msg);
});
Run Code Online (Sandbox Code Playgroud) ajax jquery getmethod internet-explorer-8 internet-explorer-7
我有下面的控制器和路线
[HttpGet]
[Route("getByEmail/{email:alpha}")]
public IHttpActionResult Get(string email)
{
var user = _userLogic.GetUserByEmail(email);
return Ok(user);
}
Run Code Online (Sandbox Code Playgroud)
但是当我打电话时
localhost/WebApp/api/user/getByEmail/fsd@sd
Run Code Online (Sandbox Code Playgroud)
它返回404未找到
I've tried
localhost/WebApp/api/user/getByEmail/?email=fsd@sd
Run Code Online (Sandbox Code Playgroud)
但它会得到所有可以有人请帮助我吗?
getmethod ×13
c# ×6
reflection ×4
.net ×3
java ×3
httpclient ×2
methods ×2
ajax ×1
asp.net-mvc ×1
detailview ×1
django ×1
enums ×1
esp32 ×1
events ×1
inheritance ×1
jquery ×1
localhost ×1
properties ×1
rest ×1
static ×1
symfony ×1
typebuilder ×1
webserver ×1