473,386 Members | 2,114 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,386 software developers and data experts.

Problem using HttpWebRequest class


Hi,

Im trying to send a files using a HTTP POST request in my Window
Application. I also have a progressbar that is being updated whil
uploading. I have managed to send a file using HTTP PUT, but not HTT
POST. I also want to send a username and password, as if it was
get?username=myuser&pass=mypass. The WebForm1.aspx is from th
following article
http://support.microsoft.com/default...;EN-US;Q323246
The code i have:

string uri = "http://localhost/WebForm1.aspx";

HttpWebRequest myrequest
(HttpWebRequest)WebRequest.Create(uri);
myrequest.SendChunked = true;
myrequest.UserAgent = "My agent";

myrequest.Method = "POST";
myrequest.ContentType = "multipart/form-data";

myrequest.AllowWriteStreamBuffering = false;

// Create a filestream
FileStream fs = new FileStream(filename, FileMode.Open
FileAccess.Read, FileShare.None);

_currentbar.Value = 0;
_currentbar.Minimum = 0;
_currentbar.Maximum = Convert.ToInt32(fs.Length);

try
{
Stream datastream = myrequest.GetRequestStream();
byte[] bytes = new byte[4096];
WriteToLog("Uploading file " + filename + " ( "
fileobj.formatSize(fs.Length) + " )", false);
currentFile.Text = Path.GetFileName(filename);
int i = fs.Read(bytes,0,bytes.Length);
while(i > 0)
{

datastream.Write(bytes, 0, bytes.Length);
updateProgressBar(i);
i = fs.Read(bytes,0,bytes.Length);

}
fs.Close();
datastream.Close();
try
{
HttpWebResponse response
(HttpWebResponse)myrequest.GetResponse();

}
catch(WebException we)
{
Console.WriteLine(we.ToString());
}
catch(NotSupportedException nse)
{
Console.WriteLine(nse.ToString());
}
}
catch(ProtocolViolationException pve)
{
Console.WriteLine(pve.ToString());
}

It wont upload the file

--
parsein
-----------------------------------------------------------------------
parseint's Profile: http://www.msusenet.com/member.php?userid=279
View this thread: http://www.msusenet.com/t-187057119

Nov 17 '05 #1
2 1709

Still having the problem, feel free to help out :

--
parsein
-----------------------------------------------------------------------
parseint's Profile: http://www.msusenet.com/member.php?userid=279
View this thread: http://www.msusenet.com/t-187057119

Nov 17 '05 #2
parseint wrote:

Hi,

Im trying to send a files using a HTTP POST request in my Windows
Application. I also have a progressbar that is being updated while
uploading. I have managed to send a file using HTTP PUT, but not HTTP
POST. I also want to send a username and password, as if it was a
get?username=myuser&pass=mypass. The WebForm1.aspx is from the
following article:
http://support.microsoft.com/default...;EN-US;Q323246

[snip]

You're not really sending a multipart/form-data request (see
http://www.faqs.org/rfcs/rfc2388.html). You simply dump the file to the
request stream (a "straight POST") -- in that case, set the
content-type according to the file's content.

Note that both options work, but the serverside certainly only expects
one or the other.

Cheers,
--
http://www.joergjooss.de
mailto:ne********@joergjooss.de
Nov 17 '05 #3

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

Similar topics

0
by: Darryl | last post by:
I'm trying to use the HttpWebRequest class to retrieve XML generated by a jsp page, but an 'The remote server returned an error: (500) Internal Server Error' exception is thrown when I call the...
2
by: Joris Janssens | last post by:
I'm trying to write a program for validating XHTML 1.1-documents against the XHTML 1.1 DTD (which is actually the same as validating an XML-file) but I always get a "(404) Not found" error. This...
6
by: Mike Koerner | last post by:
Hi, I am having problems setting the HttpWebRequest Date header. I understand that it is a restricted header and I do receive the "This header must be modified with the appropriate property." ...
6
by: Darren | last post by:
I created a class to submit an xml document to a webpage. When I use my class in a console application, everything submits correctly. However, when I attempt to use my class from an asp.net...
1
by: bliz_87 | last post by:
Hello all, I'm trying to create a simple application that does a HTTP request/response on a button click. Here's the entire code which I got from a reference book: using...
0
by: Alok yadav | last post by:
i am using the link of metacharge.com for payment gateway, when i post data throung IE it give the correct response, but when i use the .net code to post data the response is not correct.message is...
5
by: Dhananjay | last post by:
hello everyone I have a problem with xml and sql server 2k. first thing i have a website named this-> http://en.infosites.org/info/keyword. when i am entering a value for keyword like this ->...
3
by: stoogots2 | last post by:
Hi, I was using the WebClient class (to do some screen scraping) and I was forced to change to the HttpWebRequest/Response because I needed control over the http headers. I am having two problems...
1
by: topster | last post by:
Hi everyone, I have a problem I want to basically scrape a webpage using the URL and display this info into a textbox with multiline enabled. MY PROBLEM: I'm not sure where to state the URL or...
1
by: Proogeren | last post by:
I have a problem with a httpwebrequest that I am creating. The request in itself looks correct but using fiddler I see that a www-authentication header is sent along as well. The code is pasted...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.