简体   繁体   中英

Parameter Exception in HttpWebRequest BeginGetResponse Windows Phone

I send in my app POST request to some URL. When I send it for the first time, it´s OK, but when I call again method SendBookingFreeCapacity() , then I get

ApplicationUnhandledExceptionEventArgs in App.xaml in function "private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)" .

Problem is line:

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

Here is my code:

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) { }
}

Do you have some tips what can cause it? Where is problem?

Some users experience the same issue.

How to fix System.ArgumentException in HttpWebResponse?

In this case, the reason remains unknown.

System.ArgumentException: [net_WebHeaderInvalidControlChars] in Windoows Phone

In this case, the reason was non-ASCII character in the HTTP request header.

PS You should never develop on non-English OS or IDE. I'm 99% sure you get System.ArgumentException, however your "I have this name of exception localized in my language" makes both googling for solution and asking for help much more difficult.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM