WP7 通过 https 访问 Freshbooks API 不起作用

发布于 2024-10-21 19:47:32 字数 2371 浏览 0 评论 0原文

我有这段代码可以在 Windows 控制台应用程序中工作,但不能在 Windows Phone 7 中工作。我已经为 Basecamp 编写了一个使用类似代码的应用程序,但我认为问题与 FreshBooks HTTPS 有关。我刚刚花了几个小时试图让它发挥作用。

基本上,使用 var response = request.EndGetResponse(ar) as HttpWebResponse 的行会引发异常:

远程服务器返回错误:NotFound。

我可以使用curl 成功进行调用,并在控制台应用程序中使用相同的代码。所以,我知道问题不在于我的 api 令牌、FreshBooks 帐户或 xml 格式!

private static void TestFreshbooks()
{
    var url = "https://XXXXXX.freshbooks.com/api/2.1/xml-in";
    HttpWebRequest request = CreateRequest(url);

    request.BeginGetRequestStream(CreateRequestCallback,
                                 new RequestState(null)
                                 {
                                     Request = request,
                                     Callback = (args) =>
                                     {
                                         Debug.WriteLine(args);
                                     },
                                 });
}

private static void CreateRequestCallback(IAsyncResult ar)
{
    var state = ar.AsyncState as RequestState;
    var request = state.Request;

    var xml = "<request method='client.list'></request>";

    using (Stream stream = request.EndGetRequestStream(ar))
    {
        byte[] data = Encoding.UTF8.GetBytes(xml);// encoder.GetBytes(postData);
        stream.Write(data, 0, data.Length);
    }

    request.BeginGetResponse(CreateResponseCallback, state);
}

private static void CreateResponseCallback(IAsyncResult ar)
{
    try
    {
        var state = ar.AsyncState as RequestState;
        var request = state.Request;
        using (var response = request.EndGetResponse(ar) as HttpWebResponse)
        {
            using (var rs = response.GetResponseStream())
            {
                var sr = new StreamReader(rs);
                var results = sr.ReadToEnd();
                Debug.WriteLine(results);

            }
        }
    }
    catch (Exception e)
    {
        Debug.WriteLine(e);
    }
}


private static HttpWebRequest CreateRequest(string url)
{
    var request = WebRequest.Create(new Uri(url)) as HttpWebRequest;
    var token = "XXXXXXX";

    request.AllowAutoRedirect = false;
    request.UserAgent = "freshwp7test";
    request.Method = "POST";
    request.Credentials = new NetworkCredential(token, "X");

    return request;
}

I have this code which works in a windows console application, but does not work in windows phone 7. I have already written an app for Basecamp that uses similar code, but I think the problem is related to FreshBooks HTTPS. I have just spent several hours trying to make it work.

Basically the line using var response = request.EndGetResponse(ar) as HttpWebResponse throws an exception:

The remote server returned an error: NotFound.

I can make a call successfully with curl and using the same code in a console application. So, I know the problem is not with my api token, FreshBooks account or xml format!

private static void TestFreshbooks()
{
    var url = "https://XXXXXX.freshbooks.com/api/2.1/xml-in";
    HttpWebRequest request = CreateRequest(url);

    request.BeginGetRequestStream(CreateRequestCallback,
                                 new RequestState(null)
                                 {
                                     Request = request,
                                     Callback = (args) =>
                                     {
                                         Debug.WriteLine(args);
                                     },
                                 });
}

private static void CreateRequestCallback(IAsyncResult ar)
{
    var state = ar.AsyncState as RequestState;
    var request = state.Request;

    var xml = "<request method='client.list'></request>";

    using (Stream stream = request.EndGetRequestStream(ar))
    {
        byte[] data = Encoding.UTF8.GetBytes(xml);// encoder.GetBytes(postData);
        stream.Write(data, 0, data.Length);
    }

    request.BeginGetResponse(CreateResponseCallback, state);
}

private static void CreateResponseCallback(IAsyncResult ar)
{
    try
    {
        var state = ar.AsyncState as RequestState;
        var request = state.Request;
        using (var response = request.EndGetResponse(ar) as HttpWebResponse)
        {
            using (var rs = response.GetResponseStream())
            {
                var sr = new StreamReader(rs);
                var results = sr.ReadToEnd();
                Debug.WriteLine(results);

            }
        }
    }
    catch (Exception e)
    {
        Debug.WriteLine(e);
    }
}


private static HttpWebRequest CreateRequest(string url)
{
    var request = WebRequest.Create(new Uri(url)) as HttpWebRequest;
    var token = "XXXXXXX";

    request.AllowAutoRedirect = false;
    request.UserAgent = "freshwp7test";
    request.Method = "POST";
    request.Credentials = new NetworkCredential(token, "X");

    return request;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

西瓜 2024-10-28 19:47:32

昨晚花了几个小时在这上面。今天早上我花了三分钟就想好了。睡眠对你的影响是惊人的。

事实证明,Fiddler 破坏了 https 流量并搞砸了调试。我必须关闭 fiddler 并重新启动模拟器。

谢谢。

After spending hours last night on this. I figured in 3 minutes this morning. It's amazing what some sleep will do to you.

It turns out that Fiddler was breaking the https traffic and screwing up the debugging. I had to close fiddler and restart the emulator.

Thanks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文