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
thangthang 

Issue on Apex callout with .NET web service that returns PDF file as binary

Hello guys,

 

I am a newbie in apex development. Can you please help if you have any solution as per my code below?

 

I have a .NET web page (ASPX page) that reading a pdf file in webserver and write to HttpResponse as binary as following. The ASPX page using a hash key as a query string parameter to authenticate the valid request. The following 

 

Dim FullPath As String = Server.MapPath(ModuleURL)
Dim content() As Byte = Utilities.ReadBinaryFromFile(FullPath)

Response.ClearHeaders()
Response.ClearContent()
Response.ContentType = "application/pdf"
Response.AddHeader("Content-Disposition", "inline; filename=" & Path.GetFileName(FullPath))
Response.BinaryWrite(content)
Response.End()

 

If I open that on Chrome/IE, the requested pdf file opens correctly.

 

I am trying to use Apex callout and capture the pdf file as a Blob and display the Blob content as pdf in a Visualforce page as following:

 

Visualforce page:

 

<apex:page sidebar="false" showHeader="false" controller="TestGenContentController" contentType="application/pdf">

          <script>
                window.location.href = "data&colon;application/pdf;base64,{!ContentFile}";
          </script>

</apex:page>

 

Controller:

 

public class TestGenContentController {
Public String getContentFile(){
Http h = new Http();
HttpRequest req = new HttpRequest();
HttpResponse res = new HttpResponse();

req.setEndpoint(contentURL);
req.setMethod('GET');
req.setHeader('content-type', 'application/pdf');

//invoke Http request
res = h.send(req);
Blob retBlob = res.getBodyAsBlob();
String retContentFile = EncodingUtil.base64Encode(retBlob);

return retContentFile;

}

 

I alway get incorrect format of the pdf file when executing the visualforce page.

 

 

If I change the statement:

Blob retBlob = res.getBodyAsBlob();

 

to:

String retData = res.getBody();

System.Debug(strData);

 


The debug log in the body of the Respone is as follow:

 <html><head><title>Object moved</title></head><body><h2>Object moved to <a href="/aspx/Pdf/RiskProfileDefinitions.pdf">here</a>.</h2></body></html>

 

Thanks.

 

 

 

 

 

 

 

 

 

gbu.varungbu.varun

Hi

Are you adding credentials in the the header when you are sending request. You can incresase requesting time also using setTimeout(method) as setTimeout(80000).

thangthang

Greatly appriciate gbu.varun. We dont user credential authentication. Instead, we are using a hash algorithm to generate a hash key and send as a query string in the URL.

 

I suspect that my apex code need to read the Response as byte rather calling getBodyAsBlob() but I dont know how it can be done in apex. Can you please help?

 

The following are the C# code in an ASPX page that is able to callout the URL and return the file: 

 

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Timeout = 10000;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream reader = response.GetResponseStream();
HttpContext.Current.Response.ContentType = "application/pdf";
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode("myfile.pdf"));

byte[] MyBuffer = new byte[4096];
int BytesRead;
// Read the chunk of the web response into the buffer
while (0 < (BytesRead = reader.Read(MyBuffer, 0, MyBuffer.Length)))
{
HttpContext.Current.Response.OutputStream.Write(MyBuffer, 0, BytesRead);
HttpContext.Current.Response.Flush();
}

 

 

gbu.varungbu.varun

In the given code you are successfully getting response but in apex you are not making request correctly. You will have to make request successfully. I am not more aware with hash algorithm. I think you are using screte key or or API key for your service. You should pass hash key in httpRequest header.

 

 

thangthang

Thanks gbu.varun.

 

I agree that the request has not been made successfully in apex. I have used the same URL include the hash key query string in both .net and apex code.

 

In .Net I use:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

 

In Apex I use:

req.setEndpoint(url);
req.setMethod('GET');

 

I am not sure why Apex get redirect when I am trying callout the same URL. I have tried to remove the hash key query string in the URL and add to Http Request header as below but it does not help. The call out return body with invalid validation on the .NET page (i.e. rediect to my .NET error handler page).

 

req.setHeader('Hkey',hashKey);

 

Thanks.