HttpWebRequest失败,无法弄清楚为什么

时间:2012-08-11 02:56:42

标签: windows-phone-7 httpwebrequest compact-framework

我有一个WP7应用程序,我正在尝试重建一个HTTPWebRequest,我已经使用同步方法在其他地方成功编写了(粘贴在最后),但在WP7中不起作用,我假设因为我做错了什么使用这些方法的异步版本。

我认为问题源于这样一个事实:Compact Framework上的非工作代码只能发送一个bytearray [] - 我没有发送json字符串的选项。如果我在有效的代码中发送一个bytearray,我也会遇到错误。有不同的选择吗?

这是我的代码 - 这不起作用。在最后一个方法的第二行抛出异常 - “使用(var respons ...)”:

public void CreateUser()
    {
        var request = (HttpWebRequest)WebRequest.Create("http://staging.cloudapp.net:8080/api/users/");
        request.Method = "POST";
        request.ContentType = "text/json; charset=utf-8";
        request.BeginGetRequestStream(new AsyncCallback(RequestCallback), request);
    }

    private static void RequestCallback(IAsyncResult result)
    {
        HttpWebRequest request = (HttpWebRequest)result.AsyncState;

        using (Stream postStream = request.EndGetRequestStream(result))
        {
            User user = new User("Windows", "Phone", "USCA");

            Formatting formatting = new Formatting();
            JsonSerializerSettings settings = new JsonSerializerSettings();
            settings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
            string json = JsonConvert.SerializeObject(user, formatting, settings);

            byte[] byteArray = Encoding.UTF8.GetBytes(json);

            postStream.Write(byteArray, 0, json.Length);
        }

        request.BeginGetResponse(new AsyncCallback(ResponseCallback), request);
    }

    private static void ResponseCallback(IAsyncResult result)
    {
        var request = (HttpWebRequest)result.AsyncState;
        using (var response = (HttpWebResponse)request.EndGetResponse(result))
        {

            using (Stream streamResponse = response.GetResponseStream())
            {
                StreamReader reader = new StreamReader(streamResponse);
                string responseString = reader.ReadToEnd();
                reader.Close();
            }
        }
    }

此代码有效(同一请求的非紧凑框架版本):

HttpWebRequest request = HttpWebRequest.Create("http://staging.cloudapp.net/api/users/") as HttpWebRequest;
        request.Method = "POST";
        request.ContentType = "text/json";
        using (var writer = new StreamWriter(request.GetRequestStream()))
        {
            User user = new user("Other", "Guy", "USWC");
            Formatting formatting = new Formatting();
            JsonSerializerSettings settings = new JsonSerializerSettings();
            settings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
            string json = JsonConvert.SerializeObject(user, formatting, settings);

            writer.Write(json);
        }

        var response = request.GetResponse() as HttpWebResponse;
        using (var reader = new StreamReader(response.GetResponseStream()))
        {
            var responseText = reader.ReadToEnd();
            return responseText;
        }

感谢您的帮助!

3 个答案:

答案 0 :(得分:1)

看起来服务器正在响应“未找到404”。您请求的资源是否存在于服务器上?

答案 1 :(得分:1)

您的JSON是否包含任何非7位ASCII字符,正如您当前所做的那样:

byte[] byteArray = Encoding.UTF8.GetBytes(json);

postStream.Write(byteArray, 0, json.Length);

字节数可能与字符串中的字符数不同,这可能会导致格式错误的请求。

使用像Fiddler这样的东西来验证模拟器或手机上的线路实际上是多少是值得的(instructions on the Fiddler website如何做到这一点)

答案 2 :(得分:0)

嗯 - 我不确定为什么这个问题消失了。我喜欢@ RowlandShaw的建议,但我实际上并没有在json中改变任何东西。希望我能提供更好的解决方案。