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开发者_JAVA百科 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;
}
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.
精彩评论