HttpWebRequest and the POST method from a win form app

R

Rachet?

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 ;
}
}
 
B

bruce barker

you have to url encode the values

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

-- bruce (sqlwork.com)
 
S

Scott Allen

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/
 
G

Guest

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) said:
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? said:
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 ;
}
}

 
G

Guest

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 said:
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

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 ;
}
}

 
S

Scott Allen

That's great. Glad you have it working.

--s

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 said:
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

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 ;
}
}

 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top