WebClient not posting data.

  • Thread starter Thread starter Manuel
  • Start date Start date
M

Manuel

I have an asp page ("test.asp") that presents the data it receives from a
post.When I try the following code, test.asp doesn't return the values
(supposedly) posted to it. If I make a web page with a form and the values,
test.asp reports them fine.

----------------------------

Dim thePost As String = "Variable1=Value1&Variable2=Value2&Variable3=Value3"
Dim thePage As Byte()
Dim MyWebClient As New System.Net.WebClient
thePage = MyWebClient.UploadData("http://localhost/test.asp",
System.Text.Encoding.ASCII.GetBytes(thePost))

----------------------------

Does anyone have an idea why the WebClient is not posting the data?


.... I made some progress, but encountered other problems :(

I made a successful post using the following code:

----------------------------
Dim oNameValues As New System.Collections.Specialized.NameValueCollection
oNameValues.Add("Variable1", "Value1")
oNameValues.Add("Variable2", "Value2")
oNameValues.Add("Variable3", "Value3")

Dim thePage As Byte()
Dim MyWebClient As New System.Net.WebClient
thePage = MyWebClient.UploadValues("http://localhost/test.asp", "POST",
oNameValues)
----------------------------

The problem is that I cannot change the Content-Type. If I try to change the
content type with:

MyWebClient.Headers.Add("Content-Type", "multipart/form-data")

I get the following error at the .UploadValues line:

----------------------------
An unhandled exception of type 'System.Net.WebException' occurred in
system.dll

Additional information: The Content-Type header cannot be changed from its
default value for this request.
----------------------------

How can I make a successful post using "Content-Type = multipart/form-data"?




PS. Sorry for the cross-post, I first submitted it to the adonet group by
mistake.
 
Hi Manuel,
Instead of uploading the form data to the server, form variable POST it can
be achieved by writing the data in the server (ur web app).

I have given the sample in C#, with the content type
"application/x-www-form-urlencoded".

For eg:- to POST the form variable "Variable1" with its value "Value1",

add a method to write the content to the URL,

private void postData(WebRequest webRequest, string data)
{
bytes = System.Text.Encoding.ASCII.GetBytes (data);
webRequest.ContentLength = bytes.Length;

Stream outputStream = webRequest.GetRequestStream();
outputStream.Write (bytes, 0, bytes.Length);
outputStream.Close ();
}

Call this method, after setting ur content type , i.e,

the actual code shld. be,


WebRequest objWebRequest = WebRequest.Create("http://urwebsite/abc.aspx");
objWebRequest.Method = "POST";
objWebRequest.ContentType = "application/x-www-form-urlencoded";

postData = "Variable1=Value1";

postData(objWebRequest,postData);

WebResponse objResponse = objWebRequest.GetResponse();

string strResponse = "";

using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()) )
{
strResponse = sr.ReadToEnd();
sr.Close();
}

objResponse.Close();

Hope this helps u.......

Regards,
Kamal T.
 
Thank you for your suggestion.

The problem is that I have to use the System.Net.WebClient object because I
need to maintain the web session (and cookies) when I "navigate" the site
after I log in.

Since the System.Net.WebClient doesn't have a WebRequest property, I don't
know how to "link" your example with the WebClient object.
Is this possible?
 
Manuel said:
I have an asp page ("test.asp") that presents the data it receives
from a post.When I try the following code, test.asp doesn't return
the values (supposedly) posted to it. If I make a web page with a
form and the values, test.asp reports them fine. [...]

The problem is that I cannot change the Content-Type. If I try to
change the content type with:

MyWebClient.Headers.Add("Content-Type", "multipart/form-data")

I get the following error at the .UploadValues line:

----------------------------
An unhandled exception of type 'System.Net.WebException' occurred in
system.dll

Additional information: The Content-Type header cannot be changed
from its default value for this request.

WebClient uses multipart/form-data only for file uploads. Unfortunately,
there's no direct support for multipart/form-data in the FCL other than that
special case. You'll have to implement one yourself using HttpWebRequest if
you want to use multipart/form-data for another purpose.

Cheers,
 
Back
Top