views:

1298

answers:

4

i have a web service which contains a method like the following

 [WebMethod]
 public string UploadFile(byte[] bytes, string file_name)
  {

  }

i want to invoke this web service method using HttpWebRequest so that i can stream the file without buffering in memory. How can do it... i tried to invoke it as follows

HttpWebRequest hw = HttpWebRequest.Create("http://localhost/xxx/xxx.asmx/Upload") as HttpWebRequest;
hw.ContentType = "application/x-www-form-urlencoded";
hw.AllowWriteStreamBuffering = false;
hw.Method = "POST";
hw.UnsafeAuthenticatedConnectionSharing = true;
hw.UserAgent = "test type";
hw.Credentials = CredentialCache.DefaultCredentials;
FileInfo fi = new FileInfo("C:/Documents and Settings/Administrator/Desktop/a.txt");
string bytes = "bytes=";
byte[] by = UTF8Encoding.UTF8.GetBytes(bytes);
byte[] fn = UTF8Encoding.UTF8.GetBytes("&file_name=a.txt");
hw.ContentLength = by.Length+fi.Length+fn.Length;

using (Stream postStream = hw.GetRequestStream())
 {
        FileStream br = new FileStream("C:/Documents and Settings/Administrator/Desktop/a.txt", FileMode.Open, FileAccess.Read);

        postStream.Write(by, 0, by.Length);

        byte[] buffer = new byte[1024];
        int bytesRead = 0;
        while (true)
        {
            bytesRead = br.Read(buffer, 0, buffer.Length);
            if (bytesRead == 0)
                break;
            postStream.Write(buffer, 0, bytesRead);
        }
        br.Close();

        postStream.Write(fn, 0, fn.Length);
        postStream.Write(ct, 0, ct.Length);
    }
    // HERE : 
    using (HttpWebResponse response = hw.GetResponse() as HttpWebResponse)
    {

        StreamReader reader = new StreamReader(response.GetResponseStream());

         string sssdd = reader.ReadToEnd();

    }

but it show a message "The remote server returned an error: (500) Internal Server Error." while executing **// HERE : ** marked line.

btw: i am using asp.net 2.0 and i am not allowed to use proxy class as my requirement is to stream the data for large file. Thanks in advance

+1  A: 

Open up http://localhost/xxx/xxx.asmx/Upload in your browser and ASP.NET will give you an example POST requrest you'll need to send in order to invoke the method. Start from there on.

Anton Gogolev
give me an example... its not working as u said.. :-(
A: 

If it says that the remote server returned an error, then you should look on the remote server to see what the error was. Look in the Event Log on the server for events at around the time you received the error.

John Saunders
A: 

No luck...need help... something is not right in the way i am invoking the service... here is example of POST requrest

HTTP POST The following is a sample HTTP POST request and response. The placeholders shown need to be replaced with actual values.

POST /xxxx/xxxx.asmx/Upload HTTP/1.1 Host: localhost Content-Type: application/x-www-form-urlencoded Content-Length: length

bytes=string&bytes=string&file_name=string

A: 

It's been a while since I messed with this stuff, but I think you can get at the underlying HttpRequest in the proxy class. Probably alot easier than ginning up your own SOAP to POST.

Wyatt Barnett