function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
barak inbal 10barak inbal 10 

Fetch image from Rich Text Field via Https

I have C# batch which communicate with salesforce objects via SOAP API.
I want to fetch an image which is part of the Rich Text Field in salesforce custom object.
The rich text field himself is Html text and I can get the url of the image tag, but the problem is that the url himself is Https = Http secure connection:
The url of the image tag:

 https://ngam--kerensen.cs17.my.salesforce.com?ec=302&startURL=%2Fcontent%2Fsession%3Furl%3Dhttps%253A%252F%252Fngam--kerensen--c.cs17.content.force.com%252Fservlet%252FrtaImage%253Feid%253Da1Vg0000000lTkK%2526feoid%253D00N20000003jcie%2526refid%253D0EMg00000009N4I (http://ngam--kerensen.cs17.my.salesforce.com?ec=302&startURL=%2Fcontent%2Fsession%3Furl%3Dhttps%253A%252F%252Fngam--kerensen--c.cs17.content.force.com%252Fservlet%252FrtaImage%253Feid%253Da1Vg0000000lTkK%2526feoid%253D00N20000003jcie%2526refid%253D0EMg00000009N4I)

As a result of this, I can't get the resource and write it to a file as a local image for later use.
This is the C# code for fetching the image:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    bool answer = response.ContentType.StartsWith("image", StringComparison.OrdinalIgnoreCase);
     if ((response.StatusCode == HttpStatusCode.OK ||
                        response.StatusCode == HttpStatusCode.Moved ||
                        response.StatusCode == HttpStatusCode.Redirect)&&
                        response.ContentType.StartsWith("text/html", StringComparison.OrdinalIgnoreCase)){
       // if the remote file was found, download it
         using (Stream inputStream = response.GetResponseStream())
         using (Stream outputStream = File.OpenWrite(filepath))
                        {
                            byte[] buffer = new byte[8192];
                            int bytesRead;
   
                            logger.InfoFormat("Writing to file, filepath:{0}", filepath);
                            do
                            {
                                bytesRead = inputStream.Read(buffer, 0, buffer.Length);
                                logger.InfoFormat("bytesRead: {0} ", bytesRead);
                                outputStream.Write(buffer, 0, bytesRead);
                            } while (bytesRead != 0);
                            //TODO outputStream.Close();
   
                        }
                        return true;
   
                    }
                }
                catch (Exception ex)
                {
                    logger.ErrorFormat("Failed to download {0} to {1} : {2}",
                        uri, filepath, ex.Message);
                    logger.Debug("Failed to download " + uri, ex);
                }
   
                return false;



I know that the secure url using java servlet for exposing the image to the world.
The problem is that I can't get the image data and always get the following respond:

    
<script>
    if (this.SfdcApp && this.SfdcApp.projectOneNavigator) { SfdcApp.projectOneNavigator.handleRedirect('https://ngam--kerensen.cs17.my.salesforce.com?ec=302&startURL=%2Fcontent%2Fsession%3Furl%3Dhttps%253A%252F%252Fngam--kerensen--c.cs17.content.force.com%252Fservlet%252FrtaImage%253Feid%253Da1Vg0000000lTkK%2526feoid%253D00N20000003jcie%2526refid%253D0EMg00000009N4I'); }  else
    if (window.location.replace){
    window.location.replace('https://ngam--kerensen.cs17.my.salesforce.com?ec=302&startURL=%2Fcontent%2Fsession%3Furl%3Dhttps%253A%252F%252Fngam--kerensen--c.cs17.content.force.com%252Fservlet%252FrtaImage%253Feid%253Da1Vg0000000lTkK%2526feoid%253D00N20000003jcie%2526refid%253D0EMg00000009N4I');
    } else {;
    window.location.href ='https://ngam--kerensen.cs17.my.salesforce.com?ec=302&startURL=%2Fcontent%2Fsession%3Furl%3Dhttps%253A%252F%252Fngam--kerensen--c.cs17.content.force.com%252Fservlet%252FrtaImage%253Feid%253Da1Vg0000000lTkK%2526feoid%253D00N20000003jcie%2526refid%253D0EMg00000009N4I';
    }
    </script>



Any suggestion how to get the actual image data from the Rich Text Area field?
CdubCdub
We are trying to do the same thing.  Look for posts/information on Redirection, because that's what appears to be happening.
I can stream - read a text file with absolutely no problem using somewhat similar code as yours.