HttpWebRequest and the POST method from a win form app

Discussion in 'ASP .Net' started by Rachet?, Jul 14, 2004.

  1. Rachet?

    Rachet? Guest

    I am getting a "The remote server returned an error: (400) Bad
    Request." error while trying to send data to an asp page. The puzzle
    is, if I paste the string I want to send on a browser address box, it
    just go fine. From the win form app also it go fine until I happen to
    send an xml formatted string as a value. But same go fine through the
    browser.

    Any help is greatly appreciated.

    My code looks like:

    string responseString = null;
    string strPost=names[0]+"=" + values[0];

    for(int i=1;i<values.Length;i++)
    {
    strPost += ("&" +names+"=" + values);
    }

    byte[] encodedRequest = Encoding.UTF8.GetBytes(strPost);

    HttpWebRequest httpReq =
    (HttpWebRequest)WebRequest.Create(myaspPageURL)
    httpReq.ContentType =
    "application/x-www-form-urlencoded"; httpReq.Timeout = 10000;
    httpReq.KeepAlive = true;
    httpReq.UserAgent = null;
    httpReq.ContentLength = encodedRequest.Length;
    httpReq.Method = "POST";

    try
    {
    using (Stream requestStream = httpReq.GetRequestStream())
    {
    requestStream.Write(encodedRequest, 0, encodedRequest.Length);
    requestStream.Close();
    }
    }

    catch ( Exception ex )
    {
    return "Error in request : " +ex.Message;
    }

    try
    {

    HttpWebResponse httpResp = (HttpWebResponse)httpReq.GetResponse();
    using (StreamReader sr = new
    StreamReader(httpResp.GetResponseStream()) )
    {
    do
    {
    responseString = sr.ReadToEnd();
    }while(responseString=="");
    sr.Close();
    }
    httpResp.Close ();
    /return the response
    return responseString;
    }
    catch ( Exception ex1 )
    {
    //status=httpResp.StatusDescription ;
    return "Error in response : " +ex1.Message ;
    }
    }
     
    Rachet?, Jul 14, 2004
    #1
    1. Advertising

  2. Compare your example to this one and see if you can spot the problem.

    http://samples.gotdotnet.com/quickstart/util/srcview.aspx?path=/quickstart/h
    owto/samples/net/WebRequests/clientPOST.src

    --
    Regards

    John Timney
    Microsoft Regional Director
    Microsoft MVP

    "Rachet?" <> wrote in message
    news:...
    > I am getting a "The remote server returned an error: (400) Bad
    > Request." error while trying to send data to an asp page. The puzzle
    > is, if I paste the string I want to send on a browser address box, it
    > just go fine. From the win form app also it go fine until I happen to
    > send an xml formatted string as a value. But same go fine through the
    > browser.
    >
    > Any help is greatly appreciated.
    >
    > My code looks like:
    >
    > string responseString = null;
    > string strPost=names[0]+"=" + values[0];
    >
    > for(int i=1;i<values.Length;i++)
    > {
    > strPost += ("&" +names+"=" + values);
    > }
    >
    > byte[] encodedRequest = Encoding.UTF8.GetBytes(strPost);
    >
    > HttpWebRequest httpReq =
    > (HttpWebRequest)WebRequest.Create(myaspPageURL)
    > httpReq.ContentType =
    > "application/x-www-form-urlencoded"; httpReq.Timeout = 10000;
    > httpReq.KeepAlive = true;
    > httpReq.UserAgent = null;
    > httpReq.ContentLength = encodedRequest.Length;
    > httpReq.Method = "POST";
    >
    > try
    > {
    > using (Stream requestStream = httpReq.GetRequestStream())
    > {
    > requestStream.Write(encodedRequest, 0, encodedRequest.Length);
    > requestStream.Close();
    > }
    > }
    >
    > catch ( Exception ex )
    > {
    > return "Error in request : " +ex.Message;
    > }
    >
    > try
    > {
    >
    > HttpWebResponse httpResp = (HttpWebResponse)httpReq.GetResponse();
    > using (StreamReader sr = new
    > StreamReader(httpResp.GetResponseStream()) )
    > {
    > do
    > {
    > responseString = sr.ReadToEnd();
    > }while(responseString=="");
    > sr.Close();
    > }
    > httpResp.Close ();
    > /return the response
    > return responseString;
    > }
    > catch ( Exception ex1 )
    > {
    > //status=httpResp.StatusDescription ;
    > return "Error in response : " +ex1.Message ;
    > }
    > }
     
    John Timney \(Microsoft MVP\), Jul 14, 2004
    #2
    1. Advertising

  3. Rachet?

    bruce barker Guest

    you have to url encode the values

    strPost += ("&" +names+"=" + HttpUtility.UrlEncode(values));

    -- bruce (sqlwork.com)

    "Rachet?" <> wrote in message
    news:...
    > I am getting a "The remote server returned an error: (400) Bad
    > Request." error while trying to send data to an asp page. The puzzle
    > is, if I paste the string I want to send on a browser address box, it
    > just go fine. From the win form app also it go fine until I happen to
    > send an xml formatted string as a value. But same go fine through the
    > browser.
    >
    > Any help is greatly appreciated.
    >
    > My code looks like:
    >
    > string responseString = null;
    > string strPost=names[0]+"=" + values[0];
    >
    > for(int i=1;i<values.Length;i++)
    > {
    > strPost += ("&" +names+"=" + values);
    > }
    >
    > byte[] encodedRequest = Encoding.UTF8.GetBytes(strPost);
    >
    > HttpWebRequest httpReq =
    > (HttpWebRequest)WebRequest.Create(myaspPageURL)
    > httpReq.ContentType =
    > "application/x-www-form-urlencoded"; httpReq.Timeout = 10000;
    > httpReq.KeepAlive = true;
    > httpReq.UserAgent = null;
    > httpReq.ContentLength = encodedRequest.Length;
    > httpReq.Method = "POST";
    >
    > try
    > {
    > using (Stream requestStream = httpReq.GetRequestStream())
    > {
    > requestStream.Write(encodedRequest, 0, encodedRequest.Length);
    > requestStream.Close();
    > }
    > }
    >
    > catch ( Exception ex )
    > {
    > return "Error in request : " +ex.Message;
    > }
    >
    > try
    > {
    >
    > HttpWebResponse httpResp = (HttpWebResponse)httpReq.GetResponse();
    > using (StreamReader sr = new
    > StreamReader(httpResp.GetResponseStream()) )
    > {
    > do
    > {
    > responseString = sr.ReadToEnd();
    > }while(responseString=="");
    > sr.Close();
    > }
    > httpResp.Close ();
    > /return the response
    > return responseString;
    > }
    > catch ( Exception ex1 )
    > {
    > //status=httpResp.StatusDescription ;
    > return "Error in response : " +ex1.Message ;
    > }
    > }
     
    bruce barker, Jul 14, 2004
    #3
  4. Rachet?

    Scott Allen Guest

    Be careful to UrlEncode the values before you post.

    One tool I've found really useful for debugging these sorts of
    problems is Fiddler. You can compare what your code sends to the
    server with what IE sends to the server and spot the difference.

    http://www.bayden.com/Fiddler/help/

    --
    Scott
    http://www.OdeToCode.com

    On 14 Jul 2004 13:38:59 -0700, (Rachet?) wrote:

    >I am getting a "The remote server returned an error: (400) Bad
    >Request." error while trying to send data to an asp page. The puzzle
    >is, if I paste the string I want to send on a browser address box, it
    >just go fine. From the win form app also it go fine until I happen to
    >send an xml formatted string as a value. But same go fine through the
    >browser.
    >
    >Any help is greatly appreciated.
    >
    >My code looks like:
    >
    >string responseString = null;
    > string strPost=names[0]+"=" + values[0];
    >
    > for(int i=1;i<values.Length;i++)
    > {
    > strPost += ("&" +names+"=" + values);
    > }
    >
    >byte[] encodedRequest = Encoding.UTF8.GetBytes(strPost);
    >
    >HttpWebRequest httpReq =
    >(HttpWebRequest)WebRequest.Create(myaspPageURL)
    >httpReq.ContentType =
    >"application/x-www-form-urlencoded"; httpReq.Timeout = 10000;
    > httpReq.KeepAlive = true;
    > httpReq.UserAgent = null;
    > httpReq.ContentLength = encodedRequest.Length;
    > httpReq.Method = "POST";
    >
    > try
    > {
    > using (Stream requestStream = httpReq.GetRequestStream())
    > {
    > requestStream.Write(encodedRequest, 0, encodedRequest.Length);
    > requestStream.Close();
    > }
    > }
    >
    >catch ( Exception ex )
    >{
    >return "Error in request : " +ex.Message;
    >}
    >
    >try
    >{
    >
    >HttpWebResponse httpResp = (HttpWebResponse)httpReq.GetResponse();
    >using (StreamReader sr = new
    >StreamReader(httpResp.GetResponseStream()) )
    >{
    > do
    > {
    > responseString = sr.ReadToEnd();
    > }while(responseString=="");
    > sr.Close();
    >}
    > httpResp.Close ();
    > /return the response
    > return responseString;
    >}
    >catch ( Exception ex1 )
    > {
    > //status=httpResp.StatusDescription ;
    > return "Error in response : " +ex1.Message ;
    > }
    >}
     
    Scott Allen, Jul 15, 2004
    #4
  5. John,
    I tried to use the sample and also the suggestions by the Bruce and scott. I continue to get the error:
    System.Net.WebException: The remote server returned an error: (400) Bad Request.
    at System.Net.HttpWebRequest.CheckFinalStatus()
    at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
    at System.Net.HttpWebRequest.GetResponse()
    ....




    "John Timney (Microsoft MVP)" wrote:

    > Compare your example to this one and see if you can spot the problem.
    >
    > http://samples.gotdotnet.com/quickstart/util/srcview.aspx?path=/quickstart/h
    > owto/samples/net/WebRequests/clientPOST.src
    >
    > --
    > Regards
    >
    > John Timney
    > Microsoft Regional Director
    > Microsoft MVP
    >
    > "Rachet?" <> wrote in message
    > news:...
    > > I am getting a "The remote server returned an error: (400) Bad
    > > Request." error while trying to send data to an asp page. The puzzle
    > > is, if I paste the string I want to send on a browser address box, it
    > > just go fine. From the win form app also it go fine until I happen to
    > > send an xml formatted string as a value. But same go fine through the
    > > browser.
    > >
    > > Any help is greatly appreciated.
    > >
    > > My code looks like:
    > >
    > > string responseString = null;
    > > string strPost=names[0]+"=" + values[0];
    > >
    > > for(int i=1;i<values.Length;i++)
    > > {
    > > strPost += ("&" +names+"=" + values);
    > > }
    > >
    > > byte[] encodedRequest = Encoding.UTF8.GetBytes(strPost);
    > >
    > > HttpWebRequest httpReq =
    > > (HttpWebRequest)WebRequest.Create(myaspPageURL)
    > > httpReq.ContentType =
    > > "application/x-www-form-urlencoded"; httpReq.Timeout = 10000;
    > > httpReq.KeepAlive = true;
    > > httpReq.UserAgent = null;
    > > httpReq.ContentLength = encodedRequest.Length;
    > > httpReq.Method = "POST";
    > >
    > > try
    > > {
    > > using (Stream requestStream = httpReq.GetRequestStream())
    > > {
    > > requestStream.Write(encodedRequest, 0, encodedRequest.Length);
    > > requestStream.Close();
    > > }
    > > }
    > >
    > > catch ( Exception ex )
    > > {
    > > return "Error in request : " +ex.Message;
    > > }
    > >
    > > try
    > > {
    > >
    > > HttpWebResponse httpResp = (HttpWebResponse)httpReq.GetResponse();
    > > using (StreamReader sr = new
    > > StreamReader(httpResp.GetResponseStream()) )
    > > {
    > > do
    > > {
    > > responseString = sr.ReadToEnd();
    > > }while(responseString=="");
    > > sr.Close();
    > > }
    > > httpResp.Close ();
    > > /return the response
    > > return responseString;
    > > }
    > > catch ( Exception ex1 )
    > > {
    > > //status=httpResp.StatusDescription ;
    > > return "Error in response : " +ex1.Message ;
    > > }
    > > }

    >
    >
    >
     
    =?Utf-8?B?UmFjaGV0ZQ==?=, Jul 15, 2004
    #5
  6. Thank you so much again. That tool is fabulous. I was able to pinpoint the problem using it. The problem I had was that the requested page had a redirection and I needed to do a manual redirection. Therefore, I just set the autoredirect property to false and handle the redirection later.

    "Scott Allen" wrote:

    > Be careful to UrlEncode the values before you post.
    >
    > One tool I've found really useful for debugging these sorts of
    > problems is Fiddler. You can compare what your code sends to the
    > server with what IE sends to the server and spot the difference.
    >
    > http://www.bayden.com/Fiddler/help/
    >
    > --
    > Scott
    > http://www.OdeToCode.com
    >
    > On 14 Jul 2004 13:38:59 -0700, (Rachet?) wrote:
    >
    > >I am getting a "The remote server returned an error: (400) Bad
    > >Request." error while trying to send data to an asp page. The puzzle
    > >is, if I paste the string I want to send on a browser address box, it
    > >just go fine. From the win form app also it go fine until I happen to
    > >send an xml formatted string as a value. But same go fine through the
    > >browser.
    > >
    > >Any help is greatly appreciated.
    > >
    > >My code looks like:
    > >
    > >string responseString = null;
    > > string strPost=names[0]+"=" + values[0];
    > >
    > > for(int i=1;i<values.Length;i++)
    > > {
    > > strPost += ("&" +names+"=" + values);
    > > }
    > >
    > >byte[] encodedRequest = Encoding.UTF8.GetBytes(strPost);
    > >
    > >HttpWebRequest httpReq =
    > >(HttpWebRequest)WebRequest.Create(myaspPageURL)
    > >httpReq.ContentType =
    > >"application/x-www-form-urlencoded"; httpReq.Timeout = 10000;
    > > httpReq.KeepAlive = true;
    > > httpReq.UserAgent = null;
    > > httpReq.ContentLength = encodedRequest.Length;
    > > httpReq.Method = "POST";
    > >
    > > try
    > > {
    > > using (Stream requestStream = httpReq.GetRequestStream())
    > > {
    > > requestStream.Write(encodedRequest, 0, encodedRequest.Length);
    > > requestStream.Close();
    > > }
    > > }
    > >
    > >catch ( Exception ex )
    > >{
    > >return "Error in request : " +ex.Message;
    > >}
    > >
    > >try
    > >{
    > >
    > >HttpWebResponse httpResp = (HttpWebResponse)httpReq.GetResponse();
    > >using (StreamReader sr = new
    > >StreamReader(httpResp.GetResponseStream()) )
    > >{
    > > do
    > > {
    > > responseString = sr.ReadToEnd();
    > > }while(responseString=="");
    > > sr.Close();
    > >}
    > > httpResp.Close ();
    > > /return the response
    > > return responseString;
    > >}
    > >catch ( Exception ex1 )
    > > {
    > > //status=httpResp.StatusDescription ;
    > > return "Error in response : " +ex1.Message ;
    > > }
    > >}

    >
    >
     
    =?Utf-8?B?UmFjaGV0ZQ==?=, Jul 16, 2004
    #6
  7. Rachet?

    Scott Allen Guest

    That's great. Glad you have it working.

    --s

    On Fri, 16 Jul 2004 04:37:02 -0700, "Rachete"
    <> wrote:

    >Thank you so much again. That tool is fabulous. I was able to pinpoint the problem using it. The problem I had was that the requested page had a redirection and I needed to do a manual redirection. Therefore, I just set the autoredirect property to false and handle the redirection later.
    >
    >"Scott Allen" wrote:
    >
    >> Be careful to UrlEncode the values before you post.
    >>
    >> One tool I've found really useful for debugging these sorts of
    >> problems is Fiddler. You can compare what your code sends to the
    >> server with what IE sends to the server and spot the difference.
    >>
    >> http://www.bayden.com/Fiddler/help/
    >>
    >> --
    >> Scott
    >> http://www.OdeToCode.com
    >>
    >> On 14 Jul 2004 13:38:59 -0700, (Rachet?) wrote:
    >>
    >> >I am getting a "The remote server returned an error: (400) Bad
    >> >Request." error while trying to send data to an asp page. The puzzle
    >> >is, if I paste the string I want to send on a browser address box, it
    >> >just go fine. From the win form app also it go fine until I happen to
    >> >send an xml formatted string as a value. But same go fine through the
    >> >browser.
    >> >
    >> >Any help is greatly appreciated.
    >> >
    >> >My code looks like:
    >> >
    >> >string responseString = null;
    >> > string strPost=names[0]+"=" + values[0];
    >> >
    >> > for(int i=1;i<values.Length;i++)
    >> > {
    >> > strPost += ("&" +names+"=" + values);
    >> > }
    >> >
    >> >byte[] encodedRequest = Encoding.UTF8.GetBytes(strPost);
    >> >
    >> >HttpWebRequest httpReq =
    >> >(HttpWebRequest)WebRequest.Create(myaspPageURL)
    >> >httpReq.ContentType =
    >> >"application/x-www-form-urlencoded"; httpReq.Timeout = 10000;
    >> > httpReq.KeepAlive = true;
    >> > httpReq.UserAgent = null;
    >> > httpReq.ContentLength = encodedRequest.Length;
    >> > httpReq.Method = "POST";
    >> >
    >> > try
    >> > {
    >> > using (Stream requestStream = httpReq.GetRequestStream())
    >> > {
    >> > requestStream.Write(encodedRequest, 0, encodedRequest.Length);
    >> > requestStream.Close();
    >> > }
    >> > }
    >> >
    >> >catch ( Exception ex )
    >> >{
    >> >return "Error in request : " +ex.Message;
    >> >}
    >> >
    >> >try
    >> >{
    >> >
    >> >HttpWebResponse httpResp = (HttpWebResponse)httpReq.GetResponse();
    >> >using (StreamReader sr = new
    >> >StreamReader(httpResp.GetResponseStream()) )
    >> >{
    >> > do
    >> > {
    >> > responseString = sr.ReadToEnd();
    >> > }while(responseString=="");
    >> > sr.Close();
    >> >}
    >> > httpResp.Close ();
    >> > /return the response
    >> > return responseString;
    >> >}
    >> >catch ( Exception ex1 )
    >> > {
    >> > //status=httpResp.StatusDescription ;
    >> > return "Error in response : " +ex1.Message ;
    >> > }
    >> >}

    >>
    >>


    --
    Scott
    http://www.OdeToCode.com
     
    Scott Allen, Jul 16, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. NGM
    Replies:
    0
    Views:
    2,324
  2. =?Utf-8?B?RGF2ZSBCcm93bg==?=

    HttpWebRequest POST result is not the same as POST via ServerXMLHT

    =?Utf-8?B?RGF2ZSBCcm93bg==?=, Nov 8, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    2,145
    Joerg Jooss
    Nov 10, 2005
  3. Natalia
    Replies:
    4
    Views:
    790
    Natalia
    Dec 5, 2006
  4. Krist
    Replies:
    6
    Views:
    762
    Arne Vajhøj
    May 7, 2010
  5. JunkFan
    Replies:
    0
    Views:
    503
    JunkFan
    Oct 23, 2008
Loading...

Share This Page