Solved

Posting files with IdHttp

Posted on 2004-04-24
6
15,149 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

708 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

16 Experts available now in Live!

Get 1:1 Help Now