473,407 Members | 2,306 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

WebClient not posting data.

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=Value 3"
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.
Nov 18 '05 #1
3 7530
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.

"Manuel" wrote:
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=Value 3"
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.

Nov 18 '05 #2
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?
"Kamal T." <Ka****@discussions.microsoft.com> wrote in message
news:DB**********************************@microsof t.com...
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.

"Manuel" wrote:
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=Value 3"
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.

Nov 18 '05 #3
Manuel wrote:
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.
----------------------------

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


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,

--
Joerg Jooss
www.joergjooss.de
ne**@joergjooss.de
Nov 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: teej | last post by:
Hello, I am having trouble posting data to an HTTP form that includes both field information and a file (for upload). A couple of issues: 1. It would seem that WebClient.UploadFile is capable...
1
by: Jason Manfield | last post by:
What is the difference (pros and cons) between retrieving data from the web using System.Web.WebClient and using HttpWebRequest and Response to get the data? The WebClient download methods seem to...
6
by: A.M-SG | last post by:
Hi, I have an aspx page at the web server that provides PDF documents for smart client applications. Here is the code in aspx page that defines content type: Response.ContentType =...
2
by: Jeff Baker | last post by:
How does one post to an ASPX page using the WebClient when the form name is required?
1
by: jmhmaine | last post by:
I've used the WebClient class on a few projects but I wanted to know if anyone could point to the good resource for Best Practices with this object. The two things I haven't seen in sample code...
3
by: bss2004 | last post by:
Help! I'm posting a PDF Doc to a remote server using WebClient UploadData and the following code. The DOC posts fine and the server returns a positive response. If I access the remote file in...
2
by: Crirus | last post by:
How can I change a WebClient timeout in order to allow more time to a server to respond -- Cheers, Crirus ------------------------------ If work were a good thing, the boss would take it...
8
by: MaxMax | last post by:
Is it possible to tell to the WebClient to use an "automatic" encoding when doing DownloadString? The encoding of the connection is written in the header, so the WebClient should be able to sense...
2
by: MichaelSchoeler | last post by:
Hi, I'm having problems with the WebClient class regarding UTF-8 encoded data. When I access a specific webservice directly I can see the data arrives in corretly formatted UTF-8. But when I...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.