HttpWebRequest中的参数异常BeginGetResponse Windows Phone

时间:2013-01-28 12:49:44

标签: c# .net windows-phone-7 parameters httpwebrequest

我将我的应用POST请求发送到某个URL。当我第一次发送它时,没关系,但是当我再次调用方法SendBookingFreeCapacity()时,我得到了

在函数ApplicationUnhandledExceptionEventArgs的App.xaml中

"private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)"

问题在于:

webRequest.BeginGetResponse(new AsyncCallback(GetBookingFreeCapacitydResponseCallback), webRequest);

这是我的代码:

public static void SendBookingFreeCapacity() {
    var url = "https://..../getFreeCapacity";

    HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
    webRequest.Method = "POST";
    webRequest.ContentType = "application/x-www-form-urlencoded";

    webRequest.BeginGetRequestStream(
        new AsyncCallback(GetBookingFreeCapacityRequestStreamCallback), 
        webRequest);
}

private static void GetBookingFreeCapacityRequestStreamCallback(
    IAsyncResult asynchronousResult)
{
    try {
        HttpWebRequest webRequest = 
            (HttpWebRequest)asynchronousResult.AsyncState;

        string postData = string.Empty;

        System.IO.Stream postStream = 
            webRequest.EndGetRequestStream(asynchronousResult);

        postData = "<?xml version=\"1.0\"?>" 
                   + "<request>" 
                   + "<login>" + Globals.Login + "</login>" 
                   + "<password>" + Globals.Password + "</password>" 
                   + "<hotId>" + htl.hotId + "</hotId>" 
                   + "<term>" 
                   + "<from>" + dayParser(Globals.PrijezdDate) + "</from>" 
                   + "<to>" + dayParser(Globals.OdjezdDate) + "</to>" 
                   + "</term>" 
                   + "</request>";

        byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(postData);

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

        webRequest.BeginGetResponse(
            new AsyncCallback(GetBookingFreeCapacitydResponseCallback),
             webRequest); //after this I get the exception
    }
    catch (Exception ex) { }
}

private static void GetBookingFreeCapacitydResponseCallback(
    IAsyncResult asynchronousResult)
{
    try {
        HttpWebRequest webRequest =
            (HttpWebRequest)asynchronousResult.AsyncState;
        //some code...
    }
    catch (Exception ex) { }
}

你有什么可以导致它的提示吗?哪里有问题?

2 个答案:

答案 0 :(得分:1)

有些用户会遇到同样的问题。

How to fix System.ArgumentException in HttpWebResponse?

在这种情况下,原因仍然未知。

System.ArgumentException: [net_WebHeaderInvalidControlChars] in Windoows Phone

在这种情况下,原因是HTTP请求标头中的非ASCII字符。

P.S。你永远不应该在非英语操作系统或IDE上开发。我99%肯定你得到System.ArgumentException,但是你的“我的本地名称以我的语言本地化”使得谷歌搜索解决方案和寻求帮助更加困难。

答案 1 :(得分:0)

我创建了解决方案......创建新页面......