Link to home
Start Free TrialLog in
Avatar of sanjaypandey
sanjaypandey

asked on

using HttpWebRequest/HttpWebResponse to generate webrequest

Hi all,

I am trying to use HttpWebRequest/HttpWebResponse  to generate a report on the webserver...once the report is generated , I wanted to display the output.

I am not sure how to use the HttpWebResponse to wait for the report generation to complete and then call the generated report.




varReportURL = "http://abc/acweb/abc/PPM.rox?Submit&__scheduleType=sync&__overwrite=new&__outputname=/StrategicPolicyAnalysis/temp.roi&" & Server.UrlEncode(varPARAMETERS) 
 
Dim myReq As HttpWebRequest = HttpWebRequest.Create(varReportURL)
        Dim myResponse As HttpWebResponse = myReq.GetResponse
        Dim m_sPageResponded As String
        m_sPageResponded = myResponse.ResponseUri.ToString()
        'Response.Write(m_sPageResponded)
 
        Dim strm As StreamReader
 
        strm = New StreamReader(myResponse.GetResponseStream(), Encoding.ASCII)
        Dim sLine As String = ""
        While Not IsDBNull(sLine)
            Response.Write(sLine)
            sLine = strm.ReadLine()
            Console.WriteLine(sLine)
        End While
 
 
I was trying something like this but dont think this is correct.
once the report is generated.. I will using a url to display the generated report.
 
varReportURL = "http://abc/acweb/abc/temp.roi?ViewPage&format=pdf&action=view&operation=open"
Response.Write("<script>window.open('" + varReportURL + "','','_target=blank', 'width=1000,height=600');</script>")

Open in new window

Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Are you getting a response back from the server with GetResponse?

Bob
Avatar of sanjaypandey
sanjaypandey

ASKER

No, though the reports are getting generated on the server..  

I tried displaying something within the loop and outside but nothing get displayed.

Dim myReq As HttpWebRequest = HttpWebRequest.Create(varReportURL)
        Dim myResponse As HttpWebResponse = myReq.GetResponse
        Dim m_sPageResponded As String
        m_sPageResponded = myResponse.ResponseUri.ToString()
        'Response.Write(m_sPageResponded)
 
        Dim strm As StreamReader
 
        strm = New StreamReader(myResponse.GetResponseStream(), Encoding.ASCII)
        Dim sLine As String = "abc"
        Response.Write("test")
        While Not IsDBNull(sLine)
            Response.Write(sLine)
            Response.Write("test")
            sLine = strm.ReadLine()
            Console.WriteLine(sLine)
        End While

Open in new window

Are you saying that the GetResponse call freezes, and never returns?  How long does it take to generate a report?

Bob
just a couple of minutes at the most.
I am not getting any output when using those response.write statement inside  and out of the loop .... but I do see the reports getting generated on the server .

Thanks
Its definitely freezing, I removed the below code and its responding. Is there a way that I can wait for the time that report is taking to generate and then execute the next line of code that displays the report without  using the StreamReader.



strm = New StreamReader(myResponse.GetResponseStream(), Encoding.ASCII)
        Dim sLine As String = "abc"
        Response.Write("test")
        While Not IsDBNull(sLine)
            Response.Write(sLine)
            Response.Write("test")
            sLine = strm.ReadLine()
            Console.WriteLine(sLine)
        End While
The default timeout for the HttpWebRequest is 100000 milliseconds (100 seconds).  If that doesn't cover you requirement, you can raise the TimeOut value to a higher number.

Bob
No that didnt help.... any other suggestion Please
This may be a problem with the way the server expects the request.  What kind of server application are you interfacing with?

Bob
This is Actuate Reporting server that we use for reporting purpose.

Actuate 8.0
Can you see the report in a browser, if you navigate to the same URL that you are using for the HttpWebRequest?

Bob
Yes, and I can also view the report that get generated every time I run this code.
There is an HTTP debugger, called Fiddler, which might help to look at the conversation between the browser and the report server:

Fiddler - Web Debugging Proxy
http://www.fiddlertool.com/fiddler/ 

You need to duplicate the conversation functions that the browser performs.

Bob
I will give this a try .. thanks
Here is the piece of code that we have in java .. that uses java side httpresponse and request and works  just fine

If some one can help me convert this to vb.net  .

thanks


/**
   * Method fetchAndReturnFile open a provided url, and 
   * returns the byte stream of that url to the client. The appropriate
   * content type is set first.
   *
   * @param response HttpServletResponse
   * @param servletData FileProxyServletData
   */
  private void fetchAndReturnFile(HttpServletResponse response, FileProxyServletData servletData)
  {
    log.trace (servletData.requestId + ": isIgnoreIOException: " + servletData.isIgnoreIOException);
    
    URL url;
    URLConnection urlConnection;
    String contentType;
    int contentLength;
    byte bytes[] = new byte[LOCAL_BUFFER_SIZE];
    int bytesRead, bytesReadTotal = 0;
    int current;
    InputStream is;
    BufferedInputStream bis;
    OutputStream os;
    long start = 0, end = 0;
    
    try
    {
      response.reset();
      
      /* open url */
      url = new URL(servletData.protocol, servletData.host, servletData.file);
      log.trace (servletData.requestId + ": url: " + url);
      urlConnection = url.openConnection();
      urlConnection.connect();
      
      /* for http, check status */
      int responseCode = HttpServletResponse.SC_OK;
      if (STR_HTTP.equals(servletData.protocol))
      {
        HttpURLConnection httpUrlConnection = (HttpURLConnection) urlConnection;
        responseCode = httpUrlConnection.getResponseCode();
      }
      if (responseCode != HttpServletResponse.SC_OK)
      {
        servletData.isSuccess = false;
        return;
      }
      
      /* set headers */
      if (servletData.type != null)
        contentType = servletData.type;
      else
        contentType = urlConnection.getContentType();
      response.setContentType(contentType);
      log.trace (servletData.requestId + ": contentType: " + contentType);
            
      if (servletData.saveName != null)
        response.setHeader("Content-Disposition", buildContentDispositionHeaderValue(servletData));      
      
      /* prepare reader/writer */
      is = urlConnection.getInputStream();
      bis = new BufferedInputStream(is, servletData.bufferSizeInt);
      os = response.getOutputStream();
      if (servletData.bufferSize != null)
      {
        response.setBufferSize(servletData.bufferSizeInt);
        log.trace (servletData.requestId + ": buffer size: " + servletData.bufferSizeInt);
      }
      log.trace (servletData.requestId + ": is/os: " + is + "/" + os);
 
      /* fetch bytes and send to client */
      boolean isEnd = false;
      bytesRead = 0;
      start = System.currentTimeMillis();
      while (true)
      {
        bytesRead = 0;
        for (int i = 0; i < bytes.length; i++)
        {
          current = bis.read();
          if (current == -1)
          {
            isEnd = true;
            break;
          }
          bytes[i] = (byte) current;
          bytesRead++;
          bytesReadTotal++;
        }
        doWrite(servletData, os, bytes, bytesRead);
        if (isEnd) break;
        if (servletData.isIOExceptionThrown)
        {
          log.trace (servletData.requestId + ": isIOExceptionThrown true");
          servletData.isSuccess = false;
          bis.close();
          return;
        }
      }
      end = System.currentTimeMillis();
      response.setContentLength(bytesReadTotal);
      bis.close();
      log.trace (servletData.requestId + ": before flush");
      doFlush(servletData, os);
      doClose(servletData, os);
      if (servletData.isIOExceptionThrown)
      {
        log.trace (servletData.requestId + ": isIOExceptionThrown true after flush/close");
        servletData.isSuccess = false;
      }
      servletData.elapsed = end - start;
            
      log.trace (servletData.requestId + ": bytesReadTotal: " + bytesReadTotal + ", elapsed: " + servletData.elapsed + "ms");
    }
    catch (MalformedURLException mue)
    {
      log.error (servletData.requestId + ": bad url", mue);
      servletData.isSuccess = false;
    }
    catch (IOException ioe)
    {
      log.error (servletData.requestId + ": bad connection, referer: " + servletData.userAgent, ioe);
      servletData.isSuccess = false;
    }
    catch (Throwable t)
    {
      log.error (servletData.requestId + ": other exception", t);
      servletData.isSuccess = false;
    }
        
  } // fetchAndReturnFile
 
 
 
2. The client need to call that proxy and set up necessary parameters;
 
 protected void processOutData(HttpServletRequest request, HttpServletResponse response, MamsiSession mamsiSession, Properties outData)
  {
    /* session expire checking */
        HttpSession session =  request.getSession(true);
        OnlineUser user = (OnlineUser) request.getAttribute(mamsi.web.filter.Bouncer.SESSION_USER);
        boolean isSessionOk = false;
        boolean isFormValid = true;
        int mode = 0;
 
        String dateFrom = "";
        String dateTo = "";
        String reportName="";
        Properties prop = new Properties();
        Hashtable formStatus = new Hashtable();
 
 
        if (user != null)
        {
            isSessionOk = user.isSessionOK();
        }
        if (! isSessionOk)
        {
             //response.sendRedirect(MamsiServlet.encodeURL(response, URL_REDIRECT));
            mamsiSession.setRedirectUrl(URL_REDIRECT);
            return;
 
        }
 
        /* form validation */
        isFormValid = validateFormValues(outData,formStatus);
        if (DEBUG) trace(TraceLog.TRACE_LEVEL_DEBUG, "ProcessOutData(): wholeFormValid?=" + isFormValid, null);
 
        try{
            if(!isFormValid )
            {
              request.setAttribute(FORM_STATUS,formStatus);
              RequestDispatcher rd;
              rd = getServletContext().getRequestDispatcher(URL_REPORT_INDEX_VIEW);
              rd.forward(request,response);
            }
            else
            {
                 mode = Integer.parseInt(outData.getProperty(ELEMENT_MODE));
                if (DEBUG) trace(TraceLog.TRACE_LEVEL_DEBUG, "ProcessInData(): mode=" + mode, null);
 
                generateDocument(outData);
 
                /* redirect to file proxy */
                String reportFileName = buildReportFileName(this.nameOutput);
 
               session.setAttribute(FileProxyServlet.SESSION_PROTOCOL, PROTOCOL);
               session.setAttribute(FileProxyServlet.SESSION_HOST, HOST);
               session.setAttribute(FileProxyServlet.SESSION_CONTENT_TYPE, CONTENT_TYPE);
               session.setAttribute(FileProxyServlet.SESSION_FILE, reportFileName);
               session.setAttribute(FileProxyServlet.SESSION_ERROR_URL, URL_ERROR) ;
 
 
 
                    if(DEBUG)
                    {
                          trace(TraceLog.TRACE_LEVEL_DEBUG, "processOutData()setAttributeFileProxyServlet:" + FileProxyServlet.SESSION_PROTOCOL + "=" +PROTOCOL,null);
                          trace(TraceLog.TRACE_LEVEL_DEBUG, "processOutData()setAttributeFileProxyServlet:" + FileProxyServlet.SESSION_HOST + "=" +HOST,null);
                          trace(TraceLog.TRACE_LEVEL_DEBUG, "processOutData()setAttributeFileProxyServlet:" + FileProxyServlet.SESSION_CONTENT_TYPE + "=" + "application/pdf",null);
                          trace(TraceLog.TRACE_LEVEL_DEBUG, "processOutData()setAttributeFileProxyServlet:" + FileProxyServlet.SESSION_FILE + "=" + reportFileName,null);
                          trace(TraceLog.TRACE_LEVEL_DEBUG, "processOutData()setAttributeFileProxyServlet:" + FileProxyServlet.SESSION_ERROR_URL + "=" + URL_ERROR,null);
 
                    }
 
                    mamsiSession.setRedirectUrl(URI_FILE_PROXY_SERVLET);
 
            }//end else
        }catch(javax.servlet.ServletException e)
        {
            trace(TraceLog.TRACE_LEVEL_DEBUG, "ProcessOutData():after form validation, exception thrown:" + e, null);
 
        }
        catch(java.io.IOException e)
        {
           trace(TraceLog.TRACE_LEVEL_DEBUG, "ProcessOutData():after form validation, exception thrown:" + e, null);
 
       }
 
  }
 
 
  /**
   * Method generateDocument
   */
  private void generateDocument(Properties prop)
  {
    if (DEBUG) trace(TraceLog.TRACE_LEVEL_DEBUG, "generateDocument(): here", null);
    URL url = null;
    URLConnection urlConnection = null;
    int bytesAvailable, bytesRead = 0;
    Object content;
    String file = buildRequestFilename(prop);
 
    try
    {
      url = new URL(PROTOCOL, HOST, file);
      if (DEBUG) trace(TraceLog.TRACE_LEVEL_DEBUG, "generateDocument(): url: " + url, null);
      urlConnection = url.openConnection();
      urlConnection.connect();
      content = urlConnection.getContent();
      if (DEBUG) trace(TraceLog.TRACE_LEVEL_DEBUG, "generateDocument(): content length: " + urlConnection.getContentLength() + ", content type: " + urlConnection.getContentType(), null);
    }
    catch (MalformedURLException mue)
    {
      trace(TraceLog.TRACE_LEVEL_ERROR, "generateDocument(): bad url", mue);
    }
    catch (IOException ioe)
    {
      trace(TraceLog.TRACE_LEVEL_ERROR, "generateDocument(): bad connection", ioe);
    }
    catch (Exception e)
    {
      trace(TraceLog.TRACE_LEVEL_ERROR, "fetchAndReturnReport(): other exception", e);
    }
 
  } // generateDocument
 
  /**
   * Method buildReportFileName
   */
  private String buildReportFileName(String reportName)
  {
 
    StringBuffer buffer = new StringBuffer();
 
      buffer.append("/acweb/");
      buffer.append(SERVER_NAME);
      buffer.append(reportName);
      buffer.append(".roi?GetReportData&format=PDF");
 
 
    if (DEBUG) trace(TraceLog.TRACE_LEVEL_DEBUG, "buildReportFileName(): " + buffer.toString(), null);
    return buffer.toString();
 
  } // buildReportFileName
 
 
  /**
   * Method buildRequestFilename
   */
  private String buildRequestFilename(Properties prop)
  {
     StringBuffer buffer = new StringBuffer();
     int mode = Integer.parseInt(prop.getProperty(ELEMENT_MODE));
     this.setReportValues (mode);
     String dtFrom = prop.getProperty(ELEMENT_DATE_FROM);
     String dtTo = prop.getProperty(ELEMENT_DATE_TO);
     if(DEBUG)
       trace(TraceLog.TRACE_LEVEL_DEBUG, "buildRequestFilename(): dtFrom= " + dtFrom + ";dtTo=" + dtTo, null);
 
      buffer.append("/acweb/");
      buffer.append(SERVER_NAME);
      buffer.append(this.reportPath);
      buffer.append(this.reportName);
      buffer.append(".rox;");
      buffer.append(this.reportVersion);
      buffer.append("?submit&DtFrom=");
      buffer.append(dtFrom);
      buffer.append("&DtTo=");
      buffer.append(dtTo);
      buffer.append("&__scheduleType=immediate&__wait=wait&__overwrite=old&__outputname=");
      buffer.append(this.nameOutput);
 
    trace(TraceLog.TRACE_LEVEL_DEBUG, "buildRequestFilename(): file: " + buffer.toString(), null);
    return buffer.toString();
 
  }

Open in new window

What is this section doing?

             session.setAttribute(FileProxyServlet.SESSION_PROTOCOL, PROTOCOL);
               session.setAttribute(FileProxyServlet.SESSION_HOST, HOST);
               session.setAttribute(FileProxyServlet.SESSION_CONTENT_TYPE, CONTENT_TYPE);
               session.setAttribute(FileProxyServlet.SESSION_FILE, reportFileName);
               session.setAttribute(FileProxyServlet.SESSION_ERROR_URL, URL_ERROR) ;
 
Bob
this is the reply I got from the java developer :

that set some attributes in the http session for easy retrieval during page interaction. You can ignore them if your app does not need that interaction.
 that are basically the same parameters you need to build up the connection URL.
 
new URL(servletData.protocol, servletData.host, servletData.file);

Is this doing a GET or a POST?

Bob
this is the reply I got from the java developer -

It does not matter. The Get method can call the post method.
 
All I can say is "huh?".  You need to find out what the browser uses when navigating to the report URL.

Bob
looks like I am step closer... in that its not freezing up.....using this code below but its executing the code to display the report before report generation completes...    any thoughts or help


 varReportURL = "http://maroa/acweb/maroa/StrategicPolicyAnalysis/PPM.rox?Submit&__scheduleType=immediate&__overwrite=new&__outputname=/StrategicPolicyAnalysis/temp91.roi&" & Server.UrlEncode(varPARAMETERS)

        Dim myReq As HttpWebRequest = HttpWebRequest.Create(varReportURL)
        myReq.Method = WebRequestMethods.Http.Get
        Dim myResponse As HttpWebResponse = myReq.GetResponse
        Dim sline As String = ""
        Dim reader As New StreamReader(myResponse.GetResponseStream())
        Dim tmp As String = reader.ReadToEnd()
        myResponse.Close()
        varReportURL = "http://maroa/acweb/maroa/StrategicPolicyAnalysis/temp91.roi?ViewPage&format=pdf&action=view&operation=open"
        Response.Write("<script>window.open('" + varReportURL + "','','_target=blank', 'width=1000,height=600');</script>")


ASKER CERTIFIED SOLUTION
Avatar of sanjaypandey
sanjaypandey

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial