API集成WP8

时间:2014-01-05 10:44:33

标签: windows-phone-8 httpwebrequest session-cookies

我必须整合php中的供应商API。我必须执行以下步骤

1-握手将提供salt和sessionID的API,并设置cookie vendor.com ii。

2-为每个后续请求生成基于HMAC MD5的令牌,并在get请求中发送令牌。 我已经完成了上述操作,但问题是我每次请求服务器说搜索它返回token mismatch error

我的代码如下。

class APIManager
{
    CookieContainer cookieJar = new CookieContainer();
    CookieCollection responseCookies = new CookieCollection();

    private async Task<string> httpRequest(HttpWebRequest request)
    {
        string received;

        using (var response = (HttpWebResponse)(await Task<WebResponse>.Factory
            .FromAsync(request.BeginGetResponse, request.EndGetResponse, null)))
        {
            using (var responseStream = response.GetResponseStream())
            {
                using (var sr = new StreamReader(responseStream))
                {
                    cookieJar = request.CookieContainer;
                    responseCookies = response.Cookies;
                    received = await sr.ReadToEndAsync();
                }
            }
        }

        return received;
    }

    public async Task<string> Get(string path)
    {
        var request = WebRequest.Create(new Uri(path)) as HttpWebRequest;
        request.CookieContainer = cookieJar;

        return await httpRequest(request);
    }

    public async Task<string> Post(string path, string postdata)
    {
        var request = WebRequest.Create(new Uri(path)) as HttpWebRequest;
        request.Method = "POST";
        request.CookieContainer = cookieJar;

        byte[] data = Encoding.UTF8.GetBytes(postdata);
        using (var requestStream = await Task<Stream>.Factory.FromAsync(request.BeginGetRequestStream, request.EndGetRequestStream, null))
        {
            await requestStream.WriteAsync(data, 0, data.Length);
        }

        return await httpRequest(request);
    }

}

我与供应商交谈,他们说我需要保留会话cookie vendor.com ii 在上面的代码中,我将cookie jar分配给下一个请求(搜索)但仍然返回相同的错误。

在浏览器中它工作正常,下面是我在浏览器中收到的cookie的屏幕截图。此外,图像显示我的HttpWebRequest cookiejar中收到的cookie显示为计数4,但cookie集合未显示任何计数。这是问题吗? enter image description here

0 个答案:

没有答案