Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a text file, and I want to post its contents to a webserver. I would like to do it using applications that come bundled with Windows. I don't mind writing a batch script to do it. But I don't want to add new software for this task.

In unix land there is curl for this task.

Any ideas or suggestions?

Edit: This needs to work in Windows XP and Windows 7

share|improve this question… – akira May 11 '12 at 6:20

One way to send a POST request with 'bundled' components is through PowerShell. Without knowing how you would normally send it, I can only really give a general description.

Basically, use the .NET WebClient class.

$wc = New-Object System.Net.WebClient;
$wc.UploadString($url, "POST", $data);

To send the contents of a text file, read it into a variable:

$data = [System.IO.File]::ReadAllText($filename);

If you want to mimic submitting a web form, you need the following:

$wc.Headers.Add("Content-type", "application/x-www-form-urlencoded");

That normally requires key-value pairs:

$data = "uploadeddata=" + [System.IO.File]::ReadAllText($filename);

This might also help:

wc.Encoding = System.Text.Encoding.UTF8;

Example with Pastebin API:

$url = "";
$filename = "test.txt";

$api_dev_key = "a Pastebin API dev key should go here";
$api_option = "paste";
$api_paste_code = [System.Uri]::EscapeDataString([System.IO.File]::ReadAllText($filename));

$data = "api_dev_key=" + $api_dev_key + "&api_option=" + $api_option + "&api_paste_code=" + $api_paste_code;

$wc = New-Object System.Net.WebClient;
$wc.Headers.Add("Content-type", "application/x-www-form-urlencoded");
$wc.UploadString($url, "POST", $data);

All this is actually based on a C# program I was writing, so there may be a shorter way. There's no real need for 'one-liners', usually.

And before anyone suggests using Get-Content for reading the text file, Get-Content returns an array with one string per line. The POST data would be harder to build from that.

share|improve this answer
Powershell doesn't come default with Windows XP, does it? I'd need to install that first before running these scripts is that correct? – Ivan May 13 '12 at 23:50
@IvanKruchkoff Unfortunately, it doesn't. It's only bundled with desktop OSes Vista and later (I'm not sure about Server 2003). That's why it's probably a good idea to include Windows versions in your question. – Bob May 14 '12 at 0:00
I didn't realise the versions differed so much. I like your solution though and know other scenarios where it will come in handy, so I appreciate the answer. – Ivan May 14 '12 at 2:49

while Download a file via HTTP from a script in Windows was about dowloading a file from CLI you might use the same tools for uploading as well:

so, this should work (powershell):

$client = new-object
$client.uploadFile("", "C:\Full\Path\To\File.txt")
share|improve this answer
up vote 0 down vote accepted

Having found no cross-platform solution across Windows XP, Windows 7 and Windows server 2003, we took the approach of a Java client using the Apache HTTP Client libraries.


javac -cp "lib\httpclient-4.1.3.jar;lib\httpcore-4.1.4.jar;lib\httpmime-4.1.3.jar"


java -cp lib\httpclient-4.1.3.jar;lib\httpmime-4.1.3.jar;lib\httpcore-4.1.4.jar;lib\httpcore-4.1.4.jar;lib\commons-logging-1.1.1.jar;lib\commons-codec-1.4.jar;. HttpSubmitter "Username" textfile1.txt textfile2.txt ... textfilen.txt


This file uploads some files to a web server, and the returned string is a url which is then opened in internet explorer.

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.ContentBody;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.util.EntityUtils;

public class HttpSubmitter {
  public static void main(String[] args) throws Exception {
    String username = null;
    HttpClient httpclient = new DefaultHttpClient();
    httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
    MultipartEntity mpEntity = new MultipartEntity();
    int counter = 0;
    for (String filename: args) {
        // First parameter passed will be the username, the rest are files.
        if (username == null) {
            username = filename;
        } else {
            // Attach file contents to the message
            File file = new File(filename);
            ContentBody cbFile = new FileBody(file, "text/plain");
            mpEntity.addPart("userfile" + ++counter, cbFile);

    ContentBody cbUsername = new StringBody(username);
    mpEntity.addPart("username", cbUsername);
    String url = "";
    HttpPost httppost = new HttpPost(url);
    System.out.println("executing request " + httppost.getRequestLine());

    HttpResponse response = httpclient.execute(httppost);
    HttpEntity resEntity = response.getEntity();

    if (resEntity != null) {
        // Returned string will be a URL, we pass it as a url to internet explorer
        // Please keep in mind the security implications of parsing a parameter you don't control to internet explorer.
        String confirmation_page = EntityUtils.toString(resEntity);
        String ie = "\"C:\\Program Files\\Internet Explorer\\iexplore.exe\" " + confirmation_page;
        // Runtime.getRuntime().exec(ie);

    if (resEntity != null) {

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .