Pepa Zapletal
Pepa Zapletal

Reputation: 2979

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?

Upvotes: 0

Views: 453

Answers (2)

Pepa Zapletal
Pepa Zapletal

Reputation: 2979

I founded the solution...create new page...

Upvotes: 0

Soonts
Soonts

Reputation: 21926

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.

P.S. 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.

Upvotes: 1

Related Questions