如何浏览器 httpresponsemessage getmethod如何使用

I'm trying to figure out how to clear the cookies in a windows phone 8 cordova plugin.
I've been playing with the advice here
which is basically you either get the web browser instance and use a convenience method, like so:
await browserInstance.ClearCookiesAsync();
or you get the cookies from the request and discard/expire them, like so:
HttpWebRequest _webR
Uri uri = new Uri("");
CookieContainer _cookieContainer = new CookieContainer();
_webRequest = (HttpWebRequest)HttpWebRequest.Create(uri);
_webRequest.CookieContainer = this._cookieC
var cookies = this._cookieContainer.GetCookies(uri);
foreach (Cookie cookie in cookies)
cookie.Discard =
cookie.Expired =
My problem is two fold:
1) I don't know how to get the cordova browser instance in order to try the calling the ClearCookiesAsync() method
2) I'm not using an HTTPWebRequest object as I need access to set some header information, I'm using a HTTPClient object and a HTTPRequestMessage ie:
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("");
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("text/html"));
client.DefaultRequestHeaders.Add("Some custom stuff","More custom stuff");
HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Get,"/something/hello.php");
HttpResponseMessage response = await client.SendAsync(req);
response.EnsureSuccessStatusCode();
string responseBody = string.E
responseBody = await response.Content.ReadAsStringAsync();
Debug.WriteLine(responseBody);
So basically, if I could figure out how to get an instance of the browser from cordova whilst within a plugin, or, clear the cookies from a HTTPClient this would be ok.
Edit: Interesting...
Apparently you can get access to cookies via the HttpClientHandler, so the start of my code above would be like this...
HttpClientHandler handler = new HttpClientHandler();
handler.CookieContainer = new CookieContainer();
HttpClient client = new HttpClient(handler);
...but creating a new Cookie Container doesn't seem to clear the cookies?
This also looks interesting:
I played with the above link and that does indeed retrieve cookies from the HttpResponseMessage but trying to invalidate those cookies using cookie.Discard and cookie.Expired does not work.
It looks as if the answer is you have to get the cordova browser instance to discard the cookies, the above will only let you play with the cookies from the individual response in what would seem to be a read only manner :(
Similar/Duplicate questions:
解决方案 I found the easier way of doing this is using a server based "logout" url that will set-cookie invalidate your cookie, by setting the same cookie with an expiry in the past.
Basically, although android and ios can do what I wanted, it looks like you can't easily do it using the windows phone API, as I found the API "is" there, but it's hard to access via a cordova plugin for the reasons explained in the question
本文地址: &
我想弄清楚如何清除windows phone 8 cordova插件中的Cookie。
我一直在玩这里的建议,此处
这基本上是您获取Web浏览器实例并使用方便的方法,像这样:
await browserInstance.ClearCookiesAsync();
或者您从请求中获取Cookie,并丢弃/过期,如下所示:
HttpWebRequest _webR
Uri uri = new Uri(“”);
CookieContainer _cookieContainer = new CookieContainer();
_webRequest =(HttpWebRequest)HttpWebRequest.Create(uri);
_webRequest.CookieContainer = this._cookieC
var cookies = this._cookieContainer.GetCookies(uri);
foreach(Cookie中的Cookie Cookie) { cookie.Discard =
cookie.Expired = }
我的问题是两折:
1)我不知道如何获取cordova浏览器实例以尝试调用ClearCookiesAsync()方法
2)不使用HTTPWebRequest对象,因为我需要访问设置一些头信息,我使用HTTPClient对象和HTTPRequestMessage即:
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(“”);
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue(“text / html”));
client.DefaultRequestHeaders.Add(“Some custom stuff”,“More custom stuff”);
HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Get,“/ something / hello.php”);
HttpResponseMessage response = await client.SendAsync(req);
response.EnsureSuccessStatusCode();
string responseBody = string.E
responseBody = await response.Content.ReadAsStringAsync();
Debug.WriteLine(responseBody);
所以基本上,如果我能找出如何从cordova获取浏览器的一个实例,插件,或者清除HTTPClient中的Cookie,这将是确定的。
编辑:有趣...
显然,你可以通过HttpClientHandler访问cookies,所以我的代码开头就是这样...
HttpClientHandler handler = new HttpClientHandler();
handler.CookieContainer = new CookieContainer();
HttpClient client = new HttpClient(handler);
...但是创建一个新的Cookie容器似乎没有清除Cookie? >
这看起来很有趣:
但确实从HttpResponseMessage检索Cookie,但试图使用cookie.Discard和cookie.Expired使这些cookie无效。
它看起来好像答案是你必须得到cordova浏览器实例来丢弃cookie,上面的只会让你玩个人响应中的cookie似乎是一种只读方式:(
相似/重复的问题:
解决方案 我发现更简单的方法是使用一个基于服务器的“注销”网址,它将通过设置相同的cookie过期的过期设置cookie使您的cookie无效。
基本上,虽然android和ios可以做我想要的,它看起来像你不能轻易做到使用windows phone API,因为我发现的API“是”在那里,但很难通过一个cordova插件,因为 中解释的原因
本文地址: &
扫一扫关注官方微信c# - Why should I use IHttpActionResult instead of HttpResponseMessage? - Stack Overflow
Join the Stack Overflow Community
Stack Overflow is a community of 7.2 million programmers, just like you, helping each other.
J it only takes a minute:
I have been developing with WebApi and have moved on to WebApi2 where Microsoft has introduced a new IHttpActionResult Interface that seems to recommended to be used over returning a HttpResponseMessage. I am confused on the advantages of this new Interface. It seems to mainly just provide a SLIGHTLY easier way to create a HttpResponseMessage.
I would make the argument that this is "abstraction for the sake of abstraction". Am I missing something? What is the real world advantages I get from using this new Interface besides maybe saving a line of code?
Old way (WebApi):
public HttpResponseMessage Delete(int id)
var status = _Repository.DeleteCustomer(id);
if (status)
return new HttpResponseMessage(HttpStatusCode.OK);
throw new HttpResponseException(HttpStatusCode.NotFound);
New Way (WebApi2):
public IHttpActionResult Delete(int id)
var status = _Repository.DeleteCustomer(id);
if (status)
//return new HttpResponseMessage(HttpStatusCode.OK);
return Ok();
//throw new HttpResponseException(HttpStatusCode.NotFound);
return NotFound();
60.5k60226367
2,94831022
You might decide not to use IHttpActionResult because your existing code builds a HttpResponseMessage that doesn't fit one of the canned responses.
You can however adapt HttpResponseMessage to IHttpActionResult using the canned response of ResponseMessage.
It took me awhile to figure this out, so I wanted to post it showing that you don't necesarily have to choose one or the other:
public IHttpActionResult SomeAction()
IHttpActionR
//we want a 303 with the ability to set location
HttpResponseMessage responseMsg = new HttpResponseMessage(HttpStatusCode.RedirectMethod);
responseMsg.Headers.Location = new Uri("http://customLocation.blah");
response = ResponseMessage(responseMsg);
Note, ResponseMessage is a method of the base class ApiController that your controller should inherit from.
22k1298147
You can still use HttpResponseMessage.
That capability will not go away.
I felt the same way as you and argued extensively with the team that there was no need for an additional abstraction.
There were a few arguments thrown around to try and justify its existence but nothing that convinced me that it was worthwhile.
That is, until I saw
sample from Brad Wilson.
If you construct IHttpActionResult classes in a way that can be chained, you gain the ability to create a "action-level" response pipeline for generating the HttpResponseMessage.
Under the covers, this is how ActionFilters are implemented however, the ordering of those ActionFilters is not obvious when reading the action method which is one reason I'm not a fan of action filters.
However, by creating IHttpActionResults that can be explicitly chained in your action method you can compose all kinds of different behaviour to generate your response.
1,96911627
94.2k21144209
// this will return HttpResponseMessage as IHttpActionResult
return ResponseMessage(httpResponseMessage);
3,26432251
3,39411439
Here are several benefits of IHttpActionResult over HttpResponseMessage mentioned in :
Simplifies unit testing your controllers.
Moves common logic for creating HTTP responses into separate classes.
Makes the intent of the controller action clearer, by hiding the low-level details of constructing the response.
But here are some other advantages of using IHttpActionResult worth mentioning:
Respecting single responsibility principle: cause action methods to have the responsibility of serving the HTTP requests and does not involve them in creating the HTTP response messages.
Useful implementations already defined in the System.Web.Http.Results namely: Ok NotFound Exception Unauthorized BadRequest Conflict Redirect InvalidModelState ()
Uses Async and Await by default.
Easy to create own ActionResult just by implementing ExecuteAsync method.
you can use ResponseMessageResult ResponseMessage(HttpResponseMessage response) to convert HttpResponseMessage to IHttpActionResult.
7,49932758
1,71311426
This is just my personal opinion and folks from web API team can probably articulate it better but here is my 2c.
First of all, I think it is not a question of one over another. You can use them both depending on what you want to do in your action method but in order to understand the real power of IHttpActionResult, you will probably need to step outside those convenient helper methods of ApiController such as Ok, NotFound, etc.
Basically, I think a class implementing IHttpActionResult as a factory of HttpResponseMessage. With that mind set, it now becomes an object that need to be returned and a factory that produces it. In general programming sense, you can create the object yourself in certain cases and in certain cases, you need a factory to do that. Same here.
If you want to return a response which needs to be constructed through a complex logic, say lots of response headers, etc, you can abstract all those logic into an action result class implementing IHttpActionResult and use it in multiple action methods to return response.
Another advantage of using IHttpActionResult as return type is that it makes ASP.NET Web API action method similar to MVC. You can return any action result without getting caught in media formatters.
Of course,
as noted by Darrel, you can chain action results and create a powerful micro-pipeline similar to message handlers themselves in the API pipeline. This you will need depending on the complexity of your action method.
Long story short - it is not IHttpActionResult versus HttpResponseMessage. Basically, it is how you want to create the response. Do it yourself or through a factory.
14.2k23545
WebAPI basically return 4 type of object: void, HttpResponseMessage, IHttpActionResult, and other strong types. First version WebAPI returns HttpResponseMessage which is pretty straight forward HTTP response message.
IHttpActionResult was introduced by WebAPI 2 which is a kind of wrap of HttpResponseMessage. It contains ExecuteAsync method to create an HttpResponseMessage. It simplifies unit testing of your controller.
Other return type are kind of strong typed classes serialized by Web API using media formatter into the response body. The drawback was you cannot directly return an error code, such as 404. All you can do is throw an HttpResponseException error.
I'd rather implement TaskExecuteAsync interface function for IHttpActionResult. Something like:
public Task&HttpResponseMessage& ExecuteAsync(CancellationToken cancellationToken)
var response = _request.CreateResponse(HttpStatusCode.InternalServerError, _respContent);
switch ((Int32)_respContent.Code)
response = _request.CreateResponse(HttpStatusCode.InternalServerError, _respContent);
response = _request.CreateResponse(HttpStatusCode.BadRequest, _respContent);
return Task.FromResult(response);
, where _request is the HttpRequest and _respContent is the payload.
2,02632251
Your Answer
Sign up or
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Post as a guest
By posting your answer, you agree to the
Not the answer you're looking for?
Browse other questions tagged
rev .25989
Stack Overflow works best with JavaScript enabled

我要回帖

更多关于 method 的文章

 

随机推荐