Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > HttpWebRequest and the POST method from a win form app

Reply
Thread Tools

HttpWebRequest and the POST method from a win form app

 
 
Rachet?
Guest
Posts: n/a
 
      07-14-2004
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[i]+"=" + values[i]);
}

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 ;
}
}
 
Reply With Quote
 
 
 
 
John Timney \(Microsoft MVP\)
Guest
Posts: n/a
 
      07-14-2004
Compare your example to this one and see if you can spot the problem.

http://samples.gotdotnet.com/quickst...=/quickstart/h
owto/samples/net/WebRequests/clientPOST.src

--
Regards

John Timney
Microsoft Regional Director
Microsoft MVP

"Rachet?" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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[i]+"=" + values[i]);
> }
>
> 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 ;
> }
> }



 
Reply With Quote
 
 
 
 
bruce barker
Guest
Posts: n/a
 
      07-14-2004
you have to url encode the values

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

-- bruce (sqlwork.com)

"Rachet?" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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[i]+"=" + values[i]);
> }
>
> 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 ;
> }
> }



 
Reply With Quote
 
Scott Allen
Guest
Posts: n/a
 
      07-15-2004
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, http://www.velocityreviews.com/forums/(E-Mail Removed) (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[i]+"=" + values[i]);
> }
>
>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 ;
> }
>}


 
Reply With Quote
 
=?Utf-8?B?UmFjaGV0ZQ==?=
Guest
Posts: n/a
 
      07-15-2004
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(IAsyncRes ult 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/quickst...=/quickstart/h
> owto/samples/net/WebRequests/clientPOST.src
>
> --
> Regards
>
> John Timney
> Microsoft Regional Director
> Microsoft MVP
>
> "Rachet?" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) om...
> > 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[i]+"=" + values[i]);
> > }
> >
> > 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 ;
> > }
> > }

>
>
>

 
Reply With Quote
 
=?Utf-8?B?UmFjaGV0ZQ==?=
Guest
Posts: n/a
 
      07-16-2004
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, (E-Mail Removed) (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[i]+"=" + values[i]);
> > }
> >
> >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 ;
> > }
> >}

>
>

 
Reply With Quote
 
Scott Allen
Guest
Posts: n/a
 
      07-16-2004
That's great. Glad you have it working.

--s

On Fri, 16 Jul 2004 04:37:02 -0700, "Rachete"
<(E-Mail Removed)> 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, (E-Mail Removed) (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[i]+"=" + values[i]);
>> > }
>> >
>> >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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Applet to run on Win 98, Win ME, Win XP, Win Vista & Win 7 ?? Krist Java 6 05-06-2010 11:53 PM
Using the POST method with HttpWebRequest Tony ASP .Net 5 11-24-2009 09:41 AM
Convert Form Post to VB.NET HTTPWebRequest code and Catch Response JunkFan ASP .Net Web Services 0 10-23-2008 01:04 AM
Webclient or HTTPWebrequest post with file and form elements Natalia ASP .Net 4 12-05-2006 05:30 PM
HttpWebRequest POST result is not the same as POST via ServerXMLHT =?Utf-8?B?RGF2ZSBCcm93bg==?= ASP .Net 1 11-10-2005 07:49 PM



Advertisments