Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8002581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:24:14+00:00 2026-06-04T16:24:14+00:00

I have an httpWebRequest to access an XML and save it locally then read

  • 0

I have an httpWebRequest to access an XML and save it locally then read it and show it to the screen. Problem is, i have to do this for more than one “pivot item”, and the method that saves the xml is

private static void GetResponseCallback(IAsyncResult asynchronousResult)

and doesn’t support adding a new variable to it so i can dynamically name the xml (“tmp”+xmlName+”.xml”) .

So the question is: How do i push a variable in the xml name ?

public class HttpWebReqMethod
    {

        public void httpRequestMethod (string url, string xmlName)
        {     
            HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(url);
            httpRequest.ContentType = "text/xml";
            httpRequest.Method = "POST";

            httpRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), httpRequest);
        }

        private static void GetRequestStreamCallback(IAsyncResult asynchronousResult)
        {
            HttpWebRequest httpRequest = (HttpWebRequest)asynchronousResult.AsyncState;

            // End the operation
            Stream postStream = httpRequest.EndGetRequestStream(asynchronousResult);


            string postData = "";

            // Convert the string into a byte array.
            byte[] byteArray = Encoding.UTF8.GetBytes(postData);

            // Write to the request stream.
            postStream.Write(byteArray, 0, postData.Length);
            postStream.Close();

            // Start the asynchronous operation to get the response
            httpRequest.BeginGetResponse(new AsyncCallback(GetResponseCallback), httpRequest);
        }

        private static void GetResponseCallback(IAsyncResult asynchronousResult)
        {
            HttpWebRequest httpRequest = (HttpWebRequest)asynchronousResult.AsyncState;

            // End the operation
            HttpWebResponse response = (HttpWebResponse)httpRequest.EndGetResponse(asynchronousResult);
            Stream streamResponse = response.GetResponseStream();
            StreamReader streamRead = new StreamReader(streamResponse);
            string responseStream = streamRead.ReadToEnd();

            using (var store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (var istream = new IsolatedStorageFileStream(@"tmp" + xmlName + ".xml", FileMode.OpenOrCreate, store))
                {
                    using (var sw = new StreamWriter(istream))
                    {
                        sw.Write(responseStream);
                    }
                }
            }

            // Close the stream object
            streamResponse.Close();
            streamRead.Close();

            // Release the HttpWebResponse
            response.Close();
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T16:24:15+00:00Added an answer on June 4, 2026 at 4:24 pm

    Here are two things you could do:

    1. make GetResponseCallback not static, and store the xmlName in an instance variable
    2. pass a state object (anything that has a property named xmlName, and something else to identify it by) to the request, and you can get it from the AsyncState
    3. change GetResponseCallback’s function to the following, and make the whole thing a callback “factory”

      private static AsyncCallback GetResponseCallback(string xmlName)
       {
          return (IAsyncResult asynchronousResult) =>{
          HttpWebRequest httpRequest = (HttpWebRequest)asynchronousResult.AsyncState;
          // End the operation
          HttpWebResponse response = (HttpWebResponse)httpRequest.EndGetResponse(asynchronousResult);
          Stream streamResponse = response.GetResponseStream();
          StreamReader streamRead = new StreamReader(streamResponse);
          string responseStream = streamRead.ReadToEnd();
      
          using (var store = IsolatedStorageFile.GetUserStoreForApplication())
          {
              using (var istream = new IsolatedStorageFileStream(@"tmp" + xmlName + ".xml", FileMode.OpenOrCreate, store))
              {
                  using (var sw = new StreamWriter(istream))
                  {
                      sw.Write(responseStream);
                  }
              }
          }
      
          // Close the stream object
          streamResponse.Close();
          streamRead.Close();
      
          // Release the HttpWebResponse
          response.Close();
        }
      }
      

    EDIT to ADD:
    the usage then changes to

     httpRequest.BeginGetRequestStream(GetRequestStreamCallback(xmlName), httpRequest);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is kind of weird problem. I have been using HttpWebRequest for a long
I have one problem with HttpWebRequest and Uri class of .NET framework (i use
I have a problem: app throws InvalidCastException when I creating HttpWebRequest in BackgroundAgent. This
I have this webdav code thats creating folders in SharePoint: HttpWebRequest request = (System.Net.HttpWebRequest)HttpWebRequest.Create(folderAddress);
I have this code working in C#: var request = (HttpWebRequest)WebRequest.Create(https://x.com/service); request.Method = GET;
Edit: This is done in the Compact Framework, I don't have access to WebClient
I have this code: private HttpWebRequest request; private HttpWebResponse wResponse; private CookieContainer cookieContainer =
I need to pass local path to HttpWebRequest in c#. i have test.xml in
I have the following code to create an HttpWebRequest in my WP7 application. I'm
I have a task to perform an HttpWebRequest using Task<WebResponse>.Factory.FromAsync(req.BeginGetRespone, req.EndGetResponse) which can obviously

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.