473,804 Members | 4,518 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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=my user&pass=mypas s. 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.Cre ate(uri);
myrequest.SendC hunked = true;
myrequest.UserA gent = "My agent";

myrequest.Metho d = "POST";
myrequest.Conte ntType = "multipart/form-data";

myrequest.Allow WriteStreamBuff ering = false;

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

_currentbar.Val ue = 0;
_currentbar.Min imum = 0;
_currentbar.Max imum = Convert.ToInt32 (fs.Length);

try
{
Stream datastream = myrequest.GetRe questStream();
byte[] bytes = new byte[4096];
WriteToLog("Upl oading file " + filename + " ( "
fileobj.formatS ize(fs.Length) + " )", false);
currentFile.Tex t = Path.GetFileNam e(filename);
int i = fs.Read(bytes,0 ,bytes.Length);
while(i > 0)
{

datastream.Writ e(bytes, 0, bytes.Length);
updateProgressB ar(i);
i = fs.Read(bytes,0 ,bytes.Length);

}
fs.Close();
datastream.Clos e();
try
{
HttpWebResponse response
(HttpWebRespons e)myrequest.Get Response();

}
catch(WebExcept ion we)
{
Console.WriteLi ne(we.ToString( ));
}
catch(NotSuppor tedException nse)
{
Console.WriteLi ne(nse.ToString ());
}
}
catch(ProtocolV iolationExcepti on pve)
{
Console.WriteLi ne(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 1730

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=my user&pass=mypas s. 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.d e
Nov 17 '05 #3

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

Similar topics

0
1198
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 WebRequest's GetResponse() method. If I type the URL into the browser (IE), the correct XML is returned. Similarly, the correct XML is returned if I open the URL using the WebClient class' OpenRead() method. Here's the code that I'm using: //The...
2
2644
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 is the program itself : ******************************************************************** using System; using System.Xml; using System.Xml.Schema;
6
4222
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." Is there a way to make sure that the date header is sent over or a way to work around this exception? Here's a sample of the code:
6
1759
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 webpage, then it always gives me this exception The underlying connection was closed: Unable to connect to the remote server. I am using the System.Net namespace Code is below:
1
3951
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 System.Collections.Generic; using System.ComponentModel; using System.Data;
0
1196
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 some data is missing. i use the following code: public class httpClass { private string UserName; private string UserPwd;
5
1567
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 -> http://en.infosites.org/info/value now when this page opens in browser(IE or Mozila FF) it shows only xml tags with some text as contents. now i have to save this in sql server 2k i have a table in that i have field as xml tags. i want to save...
3
1509
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 that have got me stumped. I am using Visual Studio 2003, Framework 1.1. 1) Even though I set HttpWebRequest.KeepAlive=true, I am not getting a connection header with keep-alive as the value. According to everything I have read this should...
1
1040
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 how to get this information into a textbox. Please can someone help me I have this following syntax:
1
8048
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 below. I do not add any www-authentication header here so I was wondering if anyone knows how to remove it. I have used almost 2 days trying to figure this out so help would be highly appreciated. CORRECT No proxy-authenticate header is present no...
0
9706
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9579
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10577
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10332
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10320
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10077
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9150
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4299
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.