質問

I am using 3rd party services which can logged user and then remembers him until he logged out. I am not sure how they do that. You send GET request with username and password and then if you get OK response you are logged in. You can check that you are logged in by another GET method. When I tried it by browser it is okay. I don't know what they used to differentiate other requests.

I am making Windows Phone app which use these services but when I use WebRequest (HttpWebRequest) I get correct answer for logging but when I tried second request for checking if user is logged in then I get answer that he isn't. I take my helpers methods from WP project and I added to WPF project and tried it and I get same results. So I guess the problem is with WebRequests settings (maybe in header). Can anyone tell me in which WebRequest is different from browser requests? Or could you tell me which parameter I should add to header to get this working? Thanks

These are my methods for getting data from requests:

    public static async Task<string> SendGetRequestGetResponse(string url)
    {
        string result;
        var request = (HttpWebRequest)WebRequest.Create(url);
        using (var httpWebResponse = await HttpExtensions.GetResponseAsync(request))
        {
            using (var reader = new StreamReader(httpWebResponse.GetResponseStream()))
            {
                result = await reader.ReadToEndAsync();
            }
        }

        return result;
    }

    public static Task<HttpWebResponse> GetResponseAsync(this HttpWebRequest request)
    {
        var taskComplete = new TaskCompletionSource<HttpWebResponse>();
        request.BeginGetResponse(asyncResponse =>
        {
            try
            {
                var responseRequest = (HttpWebRequest)asyncResponse.AsyncState;
                var someResponse = (HttpWebResponse)responseRequest.EndGetResponse(asyncResponse);
                taskComplete.TrySetResult(someResponse);
            }
            catch (WebException webExc)
            {
                var failedResponse = (HttpWebResponse)webExc.Response;
                taskComplete.TrySetResult(failedResponse);
            }
        }, request);
        return taskComplete.Task;
    }
役に立ちましたか?

解決

They are many ways or methods to write web services and to handle sessions (logged in/logged out states). Hence, its not easy to answer without knowing how your service works.

For an example, Session managing techniques.

Browsers handle sessions in a different way. In apps, we will have to handle them manually.

First when you open your service in browser(ex chrome), open developer tools (shortcut - F12) go to Network sectionand observe each request and response and what headers are set. That gives you some idea. Then come back here and update your question with more details.

Also, you can refer to this SO question on session management

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top