SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I use this coe for save image in word document.

 System.Net.WebRequest request = System.Net.HttpWebRequest.Create(SPContext.Current.Site.Url);
 request.UseDefaultCredentials = true;
 request.PreAuthenticate = true;
 request.Credentials = CredentialCache.DefaultCredentials;

 System.Net.WebResponse response = request.GetResponse();
 HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(SignAddress);
 myReq.UseDefaultCredentials = true;
 myReq.PreAuthenticate = true;
 myReq.Credentials = CredentialCache.DefaultCredentials;

 WebResponse myResp = myReq.GetResponse();

                           //Get a stream from the webresponse
  Stream stream = myResp.GetResponseStream();

when run this in server, it is work .

but when i run this with internet(ip) i get error :

Trying to use SPWEb object that has been clesed or disposed and is no longer valid.

share|improve this question

There are a few things to try:

  1. Make sure that if you use the IP it is also added into the AAM
  2. When using DEfaultCredentials the machine from which you are trying must be added to the same Domain, otherwise it will never work (e.g. Extranet/Intranet scenarios)
  3. Always check the SPContext is NOT Null (if you just take the example in a Console like this it will never work) - you would need to rely on the Managed Client Object Model or otherwise SPCOntext has no value (example here However if this is built into a Application Page or Web Part it should be ok.

Hope it helps, C:\Marius

share|improve this answer
I check all, but don't work! – ar.gorgin May 13 '12 at 4:48
instead of using DefaultCredentials try passing the same credentials manually of the user you are logged on with while testing from your server. – Marius Constantinescu - MVP May 13 '12 at 8:39
up vote 0 down vote accepted

I replace this code for get stream. it is work for me :)

   SPFile Signfile = web.GetFile(SignAddress);
    if (Signfile.Exists)
         stream = Signfile.OpenBinaryStream();
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.