我有这个代码可以在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;
}
答案 0 :(得分:0)
昨晚花了好几个小时。我觉得今天早上3分钟。一些睡眠对你有用,这真是太棒了。
事实证明,Fiddler打破了https流量并搞砸了调试。我不得不关闭提琴手并重新启动模拟器。
感谢。