这是我的模型与json响应:
exports.getUser = function(req, res, callback) {
User.find(req.body, function (err, data) {
if (err) {
res.json(err.errors);
} else {
res.json(data);
}
});
};
Run Code Online (Sandbox Code Playgroud)
在这里,我通过http.request获得它.为什么我收到(数据)字符串而不是json?
var options = {
hostname: '127.0.0.1'
,port: app.get('port')
,path: '/users'
,method: 'GET'
,headers: { 'Content-Type': 'application/json' }
};
var req = http.request(options, function(res) {
res.setEncoding('utf8');
res.on('data', function (data) {
console.log(data); // I can't parse it because, it's a string. why?
});
});
reqA.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
reqA.end();
Run Code Online (Sandbox Code Playgroud)
我怎么能得到一个json?
MSDN代码示例说明:以下代码示例使用IsAuthenticated属性来确定当前请求是否已经过身份验证.如果尚未通过身份验证,请求将重定向到另一个页面,用户可以在其中将凭据输入Web应用程序.这是应用程序的默认页面中使用的常用技术.
这很棒,但没有细节或任何东西......
它究竟在检查什么?如何将其设置为true?
加倍努力:我在哪里可以找到更详细的文档?
ASP.NET中这些类之间有什么区别?正如我发现这些类之间没有继承关系.
下面的代码返回一个实例HttpRequestWrapper,其is a HttpRequestBase与has a HttpRequest
HttpRequestMessage request = ...;
HttpRequestBase reqBase = (request.Properties["MS_HttpContext"] as HttpContextWrapper).Request;
// do somthing with reqBase.Cookies
Run Code Online (Sandbox Code Playgroud)
看起来微软希望在从HttpRequestMessage获取cookie时惹恼我们.
是否保证request.Properties["MS_HttpContext"]永远不会为空?
或者认为在ApiController的动作中处理ajax请求.我可以通过两种不同的方式达到客户端的IP.
var ip = (request.Properties["MS_HttpContext"] as HttpContextWrapper).Request.UserHostAddress
var ip = HttpContext.Current.Request.UserHostAddress
Run Code Online (Sandbox Code Playgroud)
这两者有什么区别?
或者一般来说,我可以以不同的方式访问相同的请求/响应数据,例如Cookie,Header,Requestor Info等.什么时候用哪个?我们可以说"如果它是ajax请求,HttpRequest不能保证正常工作,因为缺少某些东西所以对于ajax请求我们应该使用HttpRequestMessage"吗?
这就是我用.NET调用服务的方式:
var requestedURL = "https://accounts.google.com/o/oauth2/token?code=" + code + "&client_id=" + client_id + "&client_secret=" + client_secret + "&redirect_uri=" + redirect_uri + "&grant_type=authorization_code";
HttpWebRequest authRequest = (HttpWebRequest)WebRequest.Create(requestedURL);
authRequest.ContentType = "application/x-www-form-urlencoded";
authRequest.Method = "POST";
WebResponse authResponseTwitter = authRequest.GetResponse();
Run Code Online (Sandbox Code Playgroud)
但是当调用此方法时,我得到:
异常详细信息:System.Net.WebException:远程服务器返回错误:(411)Length Required.
我该怎么办?
是否可以使用HttpClient.NET 4.5中的新创建HTTP HEAD请求?唯一的方法我能看到的是GetAsync,DeleteAsync,PutAsync和PostAsync.我知道HttpWebRequest-class能够做到这一点,但我想使用现代HttpClient.
我需要编写一个简单的函数,它接受一个URL并处理XML或JSON的响应,我已经检查了Sun网站https://swingx-ws.dev.java.net/servlets/ProjectDocumentList,但是HttpRequest对象是无处可寻,有可能用Java做到这一点吗?我正在写一个富客户端应用程序.
我一直在围绕REST进行一些研究.我注意到Amazon S3 API主要使用http标头作为其REST接口.这对我来说是一个惊喜,因为我假设接口主要用于请求参数.
我的问题是:我应该主要使用http标头开发我的REST接口,还是应该使用请求参数?
我想知道我应该为我的改装客户设置多少秒.
我一直在尝试这个很长一段时间,并没有很好的结果.
var myObserver = {
observe: function(subject, topic, data)
{
if (topic == "http-on-examine-response")
{
// implement later
}
else if(topic == "http-on-modify-request")
{
// implement later
}
},
QueryInterface : function (id)
{
if (id.equals(Components.interfaces["nsIObserver"]) ||
id.equals(Components.interfaces["nsISupports"]))
{
return this;
}
throw Components.results.NS_NOINTERFACE;
}
};
var obs = new Service("observer-service", "ObserverService");
obs.addObserver(myObserver, "http-on-modify-request", false);
Run Code Online (Sandbox Code Playgroud)
基本上,http-on-modify-request我知道如何检查URI,找出与之关联的窗口(如果有的话)以及其他一些东西.我无法弄清楚的是如何重定向请求,我知道这是可能的,因为我可以在发出任何请求之前获得nsIHttpChannel.
谁知道该怎么办?:/我一直试着打了几个星期,然后无处可去.
我似乎找不到任何方法在JavaFX中使用WebEngine/WebView以编程方式设置cookie .API没有给出任何关于如何获得类似HttpRequest的对象来修改标题(这是我在XML-RPC中使用的应用程序)或任何类型的cookie管理器的想法.
这个页面上的任何问题似乎都没有触及这个问题 - 有这个,但它只是在applet中修复bug时禁用cookie ,我的应用程序在桌面上btw.
我想象的唯一方法是通过请求第一页(需要带有sessionID的cookie才能正确加载),获取"拒绝访问"风格的消息,在页面上下文中执行一些javascript来设置cookie和然后刷新.这个解决方案虽然是一个糟糕的用户体验.
如何使用WebEngine设置cookie?
更新:从上面链接的问题中找到线索,我尝试了解使用CookieManager和相关API的一些示例.我找到了这个代码,然后我尝试将其合并到我的应用程序中,结果很奇怪;
MyCookieStore cookie_store = new MyCookieStore();
CookieManager cookie_manager = new CookieManager(cookie_store, new MyCookiePolicy());
CookieHandler.setDefault(cookie_manager);
WebView wv = new WebView();
Run Code Online (Sandbox Code Playgroud)
现在让我们说这样做:
String url = "http://www.google.com/";
wv.getEngine.go(url);
Run Code Online (Sandbox Code Playgroud)
在发出此请求后在Eclipse中进行调试显示cookie存储映射包含cookie:
{http://www.google.com/=[NID=67=XWOQNK5VeRGEIEovNQhKsQZ5-laDaFXkzHci_uEI_UrFFkq_1d6kC-4Xg7SLSB8ZZVDjTUqJC_ot8vaVfX4ZllJ2SHEYaPnXmbq8NZVotgoQ372eU8NCIa_7X7uGl8GS, PREF=ID=6505d5000db18c8c:FF=0:TM=1358526181:LM=1358526181:S=Nzb5yzBzXiKPLk48]}
Run Code Online (Sandbox Code Playgroud)
WebEngine只使用底层注册的cookie引擎!但等等,真的吗?让我们在发出请求之前尝试添加cookie ...
cookie_store.add(new URL(url).toURI(), new HttpCookie("testCookieKey", "testCookieValue"));
Run Code Online (Sandbox Code Playgroud)
然后我看看Wireshark中的请求......
GET / HTTP/1.1
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/535.14 (KHTML, like Gecko) JavaFX/2.2 Safari/535.14 …Run Code Online (Sandbox Code Playgroud)