Click here to Skip to main content
15,881,089 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have an issue. I am trying to access some web service through asp.net. I dont have exact code of the webservice. Input to the web service is XML query which looks something like below. output would be requested column and values in text file or excelfile.

--**eXtraSheet**
Content-Disposition: form-data; name="xmlquery"

<?xml version="1.0" encoding="UTF-8" standalone="no"?><xmlquery>  <showcolumns truncation="999">    <column order="1">nmclob</column>    <column order="2">oweroperategroup</column>    <column order="3">id</column>    <column order="4">name</column>    <column order="5">appquestid</column>    <column order="6">appquestname</column>    <column order="7">appquestinstanceid</column>    <column order="8">appquestinstancename</column>    <column order="9">tgo</column>    <column order="10">active</column>  </showcolumns>  <showaggregatecolumns/><groupby/><orderby/>  <filters>    <filter condition="AND" id="appquestid" leftbracket="" operator="EQUAL" rightbracket="">25299</filter>  </filters>  <fullcontentsearch/></xmlquery>
--**eXtraSheet**


I am trying to convert java code into C#. in Java its working perfectly. Here is what i am trying to do.. At the end I will attach the java code

private void SentPostReport(string FinalURL,string postdata)
        {
            Uri uri = new Uri(FinalURL);
            WebProxy p;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
            p = new WebProxy(Constants.Constants.WebProxyURL, Constants.Constants.WebProxyPort);
            GlobalProxySelection.Select = p;
            request.ContentType = "text/xml; encoding='utf-8'";
           //request.ContentType = "multipart/form-data; boundary=**eXtraSheet**";
            request.Method = "POST";
            request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback1), request);
            allDone.WaitOne();
        }


Here is where i am calling the async method

C#
private static void GetRequestStreamCallback1(IAsyncResult asynchronousResult)
        {
            HttpWebRequest request1 = (HttpWebRequest)asynchronousResult.AsyncState;
            Stream postStream = request1.EndGetRequestStream(asynchronousResult);
            byte[] bytearray = Encoding.UTF8.GetBytes(Constants.Constants.PostdataString.ToString());
            postStream.Write(bytearray, 0, Constants.Constants.PostdataString.Length);
            postStream.Close();
            // Start the asynchronous operation to get the response
            request1.BeginGetResponse(new AsyncCallback(GetResponseCallback), request1);
        }


and response

private static void GetResponseCallback(IAsyncResult asynchronousResult)
        {
            HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
            // End the operation
            HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
            Stream streamResponse = response.GetResponseStream();
            StreamReader streamRead = new StreamReader(streamResponse);
            string responseString = streamRead.ReadToEnd();
            // Close the stream object
            streamResponse.Close();
            streamRead.Close();
            // Release the HttpWebResponse
            response.Close();
            allDone.Set();
        }


Now if i check response string.. I am receiving complete aspx contents in HTML format. as shown below.

HTML
<LINK href="custom/css/flexiblereporting.css" rel="stylesheet" type="text/css">

<script type="text/javascript" src="/js/prototype1.5.js"></script>
<script type="text/javascript" src="/js/ZeroClipboard.js"></script>
<script type="text/javascript" src="/js/genericclipboardcopy.js"></script>

<script>

document.title="Flexible Reporting";


function selectAll(list, el){
   //alert(list);
   value=el.checked;

   //var check=document.getElementById(id);
   //check.checked=true;
   //check.checked=false;

   while(list.indexOf("|") != -1 && list.length!=1){
    id=list.substring(0,list.indexOf("|"));
    //alert(id);
    if(value==true){
     var check=document.getElementById("share"+id);
     check.checked=true;
    }else{
     var check=document.getElementById("share"+id);
     check.checked=false;
    }
    list=list.substring(list.indexOf("|")+1,list.length);
   }

}

function getReport(el){

  idView=el.value;
  if(idView>-1){
   //alert(idView);
   document.location.href="uview?call=dg.reporting.custom.RHFRAlacrity&action=editreport&viewid="+idView;
  }

}

function getSummary(baseviewfilter,reportsfilter){

  var createnewreport=$("createnewreport");
  var loading=$("loading");
  createnewreport.style.display="none";
  loading.style.display="";
  document.location.href="uview?call=dg.reporting.custom.RHFRAlacrity&action=summary&reportsfilter="+reportsfilter+"&baseviewfilter="+baseviewfilter;


}

function getSummaryExtra(baseviewfilter,reportsfilter,extra){

  var createnewreport=$("createnewreport");
  var loading=$("loading");
  createnewreport.style.display="none";
  loading.style.display="";
  document.location.href="uview?call=dg.reporting.custom.RHFRAlacrity&action=summary&reportsfilter="+reportsfilter+"&baseviewfilter="+baseviewfilter+"&"+extra;

}


Can anybody throw some light in this..

Here is the JAVA code

Java
private List<String> callAlacrityService(String serverURL, String postData)
    {
        boolean _verbose = false; // for debug, set to true
        boolean finalOk = false;
        ArrayList<String> al = new ArrayList<String>();
        boolean isPostRequest = goodStr(postData);
        DataOutputStream outputStream = null;
        BufferedReader datain = null;
        InputStreamReader isr = null;

        //boolean responseErrorDetected = false;
        try {
            URL url = new URL(serverURL);
            //20091014 w051646 gw-Connection timeouts 1
            // Per google, the connection shouldn;t actually connect until we get its stream,
            // so setting the timeouts here should work.
            // http://stackoverflow.com/questions/39391/does-new-url-openconnection-necessarily-imply-a-post
            HttpURLConnection ucUrl = (HttpURLConnection)url.openConnection();
            //
            if(propTimeoutConnect > 0) { ucUrl.setConnectTimeout(propTimeoutConnect); }
            if(propTimeoutRead > 0) { ucUrl.setReadTimeout(propTimeoutRead); }
            //
            ucUrl.setUseCaches(false);
            ucUrl.setDoOutput(true);

            if(isPostRequest) {
                ucUrl.setRequestMethod("POST");
                ucUrl.setRequestProperty("Content-Type", "multipart/form-data; boundary=**eXtraSheet**");
            }
            else {
                ucUrl.setRequestMethod("GET");
            }

            ucUrl.connect();

            //---------------
            // if POST, then send the data now
            if(isPostRequest) {
                outputStream = new DataOutputStream(ucUrl.getOutputStream());
                outputStream.writeBytes(postData);
                outputStream.flush();
                outputStream.close();
                outputStream = null;
            }

            //---------------
            //
            // Read response from Alacrity as separate lines, into a list
            //
            isr = new InputStreamReader(ucUrl.getInputStream());
            datain = new BufferedReader(isr);
            String line;

            if(_verbose) { LOG("  !-------------------------"); }
            // zero out our line/char counters before starting read
            int lastNumLinesRead = 0;
            int lastNumCharsRead = 0;

            while((line = datain.readLine()) != null) {
                if(_verbose) { LOG("  !" + line); }
                //
                //TODO: some errors are due to HTTP probs-- 500, 404, etc.  Can we get the resp code from response?
                //
                //-don't do this in normal case as it is a perf issue--//resptext += line;
                //
                // add line to retval array
                al.add(line);
                lastNumLinesRead++;
                if(line!=null) {
                    lastNumCharsRead+=line.length();
                }
            }
            // persist status to member vars before logging
            lastHttpRc = ucUrl.getResponseCode();
            lastHttpRcText = ucUrl.getResponseMessage();
            LOG("  !---- rc=[" + lastHttpRc + "][" + lastHttpRcText + "]-----");
            LOG("  !---- numLinesRead=[" + lastNumLinesRead + "] numCharsRead=[" + lastNumCharsRead + "]-----");
            datain.close();
            datain = null;
            isr.close();
            isr = null;
            ucUrl.disconnect();       
        }
        catch (SocketTimeoutException ste) {
            // 20091014 w051646 gw-Connection timeouts 1
            LOG("  *** SocketTimeoutException on request to Alacrity, failed connection.");
            LOG("  *** Timeouts: propTimeoutConnect=["+propTimeoutConnect+"] propTimeoutRead=["+propTimeoutRead+"]");
            LOG("  *** " + ste.getMessage());
            al = null;
            finalOk = false;
        }
        catch (MalformedURLException e) {
            LOG("MalFormedException6 " + e.getMessage());
            al = null;
            finalOk = false;
        }
        catch (Exception e) {
            LOG("Exception7a: catchall exception in alacrity connect! " +  e.getMessage());
            al = null;
            finalOk = false;
        }
        finally {
            try {
                if(outputStream!=null) { outputStream.close(); }
                if(datain!=null) {datain.close(); }
                if(isr!=null) { isr.close(); }
            }
            catch(Exception e2) {
                LOG("  ##callAlacrityService - exception in finally block! " + e2.getMessage());
            }
        }
        if(al!=null && !al.isEmpty()) {
            finalOk = true;
        }
        return al;
    }
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900