473,770 Members | 2,104 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# app: simulate form post in a command line application

7 New Member
Hello,

I am writing a C# command line application that uploads files to a website. Basically, imagine this form in a web application:

[HTML]<form method="post" enctype="mulipa rt/form-data" action="http://www.myfilestora ge.com/">
<input type="text" name="documentI d" value="123" />
<input type="file" name="document" value="C:\myfil e.doc" />
</form>[/HTML]

I need the same thing to happen in my command line application as what would happen in the web application when that form gets submitted. In other words, I have to make a request to a specific URL with two parameters: one text parameter and one file parameter. Does anyone know how to do this?

Here's what I have so far:

Expand|Select|Wrap|Line Numbers
  1. void UploadDocument(int documentId, byte[] documentBytes)
  2. {
  3.   //create a new request
  4.   HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.myfilestorage.com/");
  5.   request.Method = "POST";
  6.   request.ContentType = "multipart/form-data";
  7.  
  8.   //write the parameters to the request
  9.   Stream requestStream = request.GetRequestStream();
  10.   /* ???? */
  11.   requestStream.Close();
  12.  
  13.   //get the response to the request
  14.   HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  15.   response.Close();
  16. }
Jan 12 '08 #1
1 4514
amphibian1
7 New Member
I've figured out a solution. Here it is:

Expand|Select|Wrap|Line Numbers
  1. void UploadDocument(int documentId, string fileName, byte[] fileBytes)
  2. {
  3.   //create a new request
  4.   string boundary = "AaB03x";
  5.   HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.myfilestorage.com/");
  6.   request.Method = "POST";
  7.   request.ContentType = "multipart/form-data; boundary=" + boundary + ";";
  8.  
  9.   byte[] beforeFileBytes = Encoding.UTF8.GetBytes(
  10.     "--" + boundary + "\r\n" +
  11.     "Content-Disposition: form-data; name=\"documentId\"\r\n\r\n" +
  12.     documentId + "\r\n" +
  13.     "--" + boundary + "\r\n" +
  14.     "Content-Disposition: form-data; name=\"document\"; filename=\"" + fileName + "\"\r\n" +
  15.     "Content-Type: application/octet-stream\r\n" +
  16.     "Content-Transfer-Encoding: binary\r\n\r\n");
  17.  
  18.   byte[] afterFileBytes = Encoding.UTF8.GetBytes("\r\n" +
  19.     "--" + boundary + "--" + "\r\n");
  20.  
  21.   byte[] postData = new byte[beforeFileBytes.Length + fileBytes.Length + afterFileBytes.Length];
  22.  
  23.   Stream stream = new MemoryStream(postData);
  24.   stream.Write(beforeFileBytes, 0, beforeFileBytes.Length);
  25.   stream.Write(fileBytes, 0, fileBytes.Length);
  26.   stream.Write(afterFileBytes, 0, afterFileBytes.Length);
  27.   stream.Close();
  28.  
  29.   //write the post data to the request
  30.   request.ContentLength = postData.Length;
  31.   Stream requestStream = request.GetRequestStream();
  32.   this.WriteToStream(requestStream, postData);
  33.   requestStream.Close();
  34.  
  35.   //get the response
  36.   WebResponse response = request.GetResponse();
  37.   response.Close();
  38. }
Jan 14 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
7786
by: Bill Cohagan | last post by:
I'm writing a console app (in C#) and I want to be able to redirect the standard input/output streams when it's run at a command prompt. IOW I want to support the "<" and ">" redirection syntax. The obvious way to do this is by using the static Console type properties, In and Out. When trying to debug the app in the IDE however, this doesn't appear to work. I've edited the project properties and added the necessary text to the "command...
1
1645
by: Fred Iannon | last post by:
All, I have developed an application that needs to run in one of two modes: (1) IF NO command line parms are provided I would like it to run as a Window form application (i.e. /target:winexe) with a Main form, etc. and with execution returing to the command line AS SOON AS the program STARTS (2) If a command line parm IS provided I would
1
5387
by: Oz | last post by:
This is long. Bear with me, as I will really go through all the convoluted stuff that shows there is a problem with streams (at least when used to redirect stdout). The basic idea is that my application (VB.NET) will start a process, redirect its stdout and capture that process' output, displaying it in a window. I've written a component for this, and a test application for the component. It allows me to specify a command to execute,...
2
2772
by: mcl chan | last post by:
Hello all, I'm using below code for c# app to call another app { System.Diagnostics.Process proc = new System.Diagnostics.Process(); proc Enable.RaisingEvents=false; proc StartInfo.Filename="anotherapp.exe" ; proc StartInfor.Arguments="1234"; proc Start();
11
29739
by: Peter Steele | last post by:
I have a Windows application written in C# that I want to return a non-zero exit code when it it run. The problem is that as a Windows application, there doesn't seem to be a way to control this. The value returned by the Main function has no impact on the value returned by the application. Windows applications exit immediately, leaving their windows still open. Is there a way to change the value a Windows application exits with. Basically...
4
7084
by: Al Cadalzo | last post by:
I'm trying to simulate a form post (i.e. Method="POST"). The FORM POST I'm trying to simulate is similar to this: <FORM NAME=SearchForm METHOD=POST ACTION=Search> <SELECT name="criteriaA" > <OPTION VALUE=1>AAAAAAAAAAA</OPTION> <OPTION VALUE=2>BBBBBBBBBBBB</OPTION> </SELECT> ....
15
2792
by: Michael C | last post by:
Is it possible to have an app that is both a console app and a windows app? If there are no command line switches then it will run as a console app, if there are command line switches then it will be a windows app. If I make it a windows app then I can't write to the console. If I make it a console app then it starts a console if the user runs it from the start menu so from what I see it's not possible. Thanks in advance, Michael
3
10115
by: Alex | last post by:
Hello, I'm wroting a console utility in VB 2005, and I need the ability to pass parameters to the application when the program runs. For example, if my program is called testing.exe, I need to be able to run this: c:\testing.exe c:\test ....and have 'c:\test' passed to the program as a variable. Most of my VB books focus on web forms or windows forms, but not much details on console
1
5969
by: TuckerRoth | last post by:
This is my first post on this site, so I don't know the rules. Please correct me if this gets to the wrong folks...etc. PROBLEM: Many of the apps that I am writing need to be able to run 2 ways: They might be invoked from the command-line with parameters so that they do not show UI, but accomplish some task; OR they might be double-clicked, in which case they show a UI (so that the User can configure settings for the app...etc.). To...
0
9591
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
9425
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
10228
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
10057
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...
0
9869
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...
1
7415
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5312
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3575
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2816
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.