Solved

Posting files with IdHttp

Posted on 2004-04-24
6
15,206 Views
Last Modified: 2012-05-04
Greetings..

I'm having problems with posting files witch over 3.5mb to IIS servers with the Indy IdHttp component.
I get this error: "Socket Error # 10054 Connection reset by peer"
In this code (witch is a part of idhttp)
---------
if (ARequest.Method in [hmPost, hmPut]) then
begin
  WriteStream(ARequest.Source, True, false);  <------this line
end;
---------
I've tried to post to apche server and that works... I find this very strange because there is no request made on my iis server when my app failes.

Here is the code I use to call the IdHttp.post function:
----------
formData := TIdMultiPartFormDataStream.Create;
formData.AddFile('File',fileName,'application/octet-stream');
HttpObject.Port := Port;
response := HttpObject.Post(posturl,formData);
-----------

I'm using "indy internet direct 9.0", Delphi 7 Enterprise on Windows XP.

Hope someone can help me.

Best regards,
Thorgeir
0
Comment
Question by:poxy
6 Comments
 
LVL 2

Expert Comment

by:PatrickMMartin
ID: 10909916
Well, using ASP.NET (my recommendation ) it seems to work fine with Indy:
you do need to check the parameters and the form field name.
The ASP.NET file saves the request so you should be able to see where the problem is.

Check this out : you need to put the test.aspx file in an IIS virtual directory and enter the URL

MainF.pas -----

unit MainF;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
  IdHTTP, StdCtrls, OleCtrls, SHDocVw;

type
  TForm1 = class(TForm)
    IdHTTP1: TIdHTTP;
    btnPost: TButton;
    ebURL: TEdit;
    WebBrowser1: TWebBrowser;
    memResponse: TMemo;
    procedure btnPostClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses
 IdMultiPartFormData
 ;

procedure TForm1.btnPostClick(Sender: TObject);
var
  formData : TIdMultiPartFormDataStream;
begin

  formData := TIdMultiPartFormDataStream.Create;
  formData.AddFile('File1', 'MainF.dfm', 'text/plain');
  idHTTP1.Port := 80;
  IdHTTP1.Request.Referer := ebURL.Text;
  memResponse.Lines.Text := idHTTP1.Post(ebURL.Text, formData);
  memResponse.Lines.SaveToFile('c:\temp\response.html');
  WebBrowser1.Navigate('c:\temp\response.html');

end;

end.


MainF.dfm --------

object Form1: TForm1
  Left = 192
  Top = 108
  Width = 696
  Height = 480
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object btnPost: TButton
    Left = 8
    Top = 16
    Width = 75
    Height = 25
    Caption = 'btnPost'
    TabOrder = 0
    OnClick = btnPostClick
  end
  object ebURL: TEdit
    Left = 112
    Top = 16
    Width = 377
    Height = 21
    TabOrder = 1
    Text = 'http://localhost/IDHTTPPost/test.aspx'
  end
  object WebBrowser1: TWebBrowser
    Left = 8
    Top = 232
    Width = 641
    Height = 201
    TabOrder = 2
    ControlData = {
      4C00000040420000C61400000000000000000000000000000000000000000000
      000000004C000000000000000000000001000000E0D057007335CF11AE690800
      2B2E126208000000000000004C0000000114020000000000C000000000000046
      8000000000000000000000000000000000000000000000000000000000000000
      00000000000000000100000000000000000000000000000000000000}
  end
  object memResponse: TMemo
    Left = 8
    Top = 48
    Width = 633
    Height = 177
    TabOrder = 3
  end
  object IdHTTP1: TIdHTTP
    MaxLineAction = maException
    ReadTimeout = 0
    AllowCookies = True
    ProxyParams.BasicAuthentication = False
    ProxyParams.ProxyPort = 0
    Request.ContentLength = -1
    Request.ContentRangeEnd = 0
    Request.ContentRangeStart = 0
    Request.ContentType = 'text/html'
    Request.Accept = 'text/html, */*'
    Request.BasicAuthentication = False
    Request.UserAgent = 'Mozilla/3.0 (compatible; Indy Library)'
    HTTPOptions = [hoForceEncodeParams]
    Left = 112
    Top = 65520
  end
end

test.aspx -------


<%@ Page Language="C#" autoeventwireup="True" %>
<script runat="server">

    void Page_Load()
    {
      Request.SaveAs(@"c:\temp\request.txt", true);
   
       if (File1.PostedFile != null)
       {
          try
          {
             File1.PostedFile.SaveAs(@"c:\temp\" + Text1.Value);
             Span1.InnerHtml = "File uploaded successfully to <b>c:\\temp\\" +
                               Text1.Value + "</b> on the Web server";
          }
          catch (Exception exc)
          {
             Span1.InnerHtml = "Error saving file <b>c:\\temp\\" +
                               Text1.Value + "</b><br>" + exc.ToString();
          }
       }
   
    }
   
   
    void Button1_Click(object Source, EventArgs e)
    {
   
       if (Text1.Value == "")
       {
          Span1.InnerHtml = "Error: you must enter a file name";
          return;
       }
   
    }

</script>
<html>
<head>
</head>
<body>
    <h3>HtmlInputFile Sample
    </h3>
    <form enctype="multipart/form-data" runat="server">
        Select File to Upload:
        <input id="File1" type="file" runat="server" />
        <p>
            Save as filename (no path):
            <input id="Text1" type="text" value="loaded.tmp" runat="server" />
        </p>
        <p>
            <input id="Button1" type="button" value="Upload" runat="server" onserverclick="Button1_Click" />
            <span id="Span1" style="FONT: 8pt verdana" runat="server">
            <p>
            </p>
            </span>
        </p>
        <p>
        </p>
        <p>
        </p>
        <p>
        </p>
    </form>
</body>
</html>
0
 
LVL 1

Expert Comment

by:tanghz
ID: 10914485
How long will it take to finish uploading the full file ? I think this should be a problem with your configuation of Web Sever. Change the connection time out longer and have a try.

What is the service program on the server side to accept the uploading file?


IdHTTP1.Request.ContentType
Double chech this property from both sides. It will cause problems when not properly set up,.


0
 
LVL 4

Expert Comment

by:ceoworks
ID: 10931954
Hi, i think that it should be a problem with IIS or ASP. I don't know much about IIS but there may be a setting which defines the maximum upload file size. I developed that kind or program before which sends the files from Delphi client to the PHP script. I was using Apache as a server. I faced with the same problem(about 2MB). After changed the php.ini setting, everything worked fine.

Regards,
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 4

Expert Comment

by:ceoworks
ID: 10931977
or it may be related with the timeout period..
0
 
LVL 2

Accepted Solution

by:
PatrickMMartin earned 400 total points
ID: 10932875
If the web server returned a serious error (500) Indy will throw an exception (which you should see). If there was literally no response, you could find out by stepping through the Indy code that reads the response (it's a bit awful).

If there are suspicions that the file size is a problem, test with varying sizes (ASP.NET defaults to a max request of around 4Mb, I believe ). However excessice file sizes will result in an immediate HTTP response, as it will read the content-length header.
This would not lead to a connection timeout.

Is the target you are posting to working?
Can you create a simple web page with a form and an input type="file" to attempt to post to it by hand?
0
 

Author Comment

by:poxy
ID: 10939134
Well the problem was the 4mb max file size in asp.net so thanks PatrickMMartin.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Intraweb download file link ? 1 132
How to renew a Delphi rad-studio licence? 5 63
Delphi: Connect to running MS Outlook 4 77
Tvertscrollbox like a whatsapp layout 5 26
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now