How to use the HttpWebRequest object

原创 2004年07月08日 16:48:00

This article was previously published under Q313126
This article describes how to use the HttpWebRequest object and the HttpWebResponse object from the "System.Net" namespace to modify the urn:schemas:mailheader:subject property for an item in Microsoft Exchange 2000 Server in Microsoft Visual C# .NET.
To modify the urn:schemas:mailheader:subject property for an item in Visual C# .NET, follow these steps:
Start Microsoft Visual Studio .NET.
On the File menu, point to New, and then click Project.
In the Visual C# Projects types list, click Console Application.

By default, Class1.cs is created.
In the code window, replace the code with the following:using System;
using System.Net;
using System.IO;        

namespace WebDavNET
   /// <summary>
   /// Summary description for Class1.
   /// </summary>
   class Class1
      static void Main(string[] args)
            // TODO: Replace with the URL of an object in Exchange Server
            string sUri = "http://ExchServer/Public/MyFolder/Test.EML";

            System.Uri myUri = new System.Uri(sUri);
            HttpWebRequest HttpWRequest = (HttpWebRequest)WebRequest.Create(myUri);

            string sQuery;
            sQuery = "<?xml version='1.0'?>" +
               "<a:propertyupdate xmlns:a='DAV:' " +
               "xmlns:m='urn:schemas:mailheader:'>" +
               "<a:set><a:prop>" +
               "<m:subject>" + "ModifiedSubject" + "</m:subject>" +
               "</a:prop></a:set>" +

            // Set Credentials
            // TODO: Replace with appropriate user credential
            NetworkCredential myCred = new NetworkCredential(@"DomainName/UserName", "Password");
            CredentialCache myCredentialCache = new CredentialCache();
            myCredentialCache.Add(myUri, "Basic", myCred);
            HttpWRequest.Credentials = myCredentialCache;
            //Uncomment the following statement and comment the previous 4 statements if you
            //use Integrated Windows authentication
            //httpWRequest.Credentials = CredentialCache.DefaultCredentials

            //Note In Basic type authentication, the username and the password are sent as base64-encoded text, which is
            //easily decoded. Microsoft recommends that you use Basic over SSL to help protect the username and the password.

            // Set Headers
            HttpWRequest.KeepAlive = false;
            HttpWRequest.Headers.Set("Pragma", "no-cache");
            HttpWRequest.Headers.Set("Translate", "f");
            HttpWRequest.ContentType =  "text/xml";
            HttpWRequest.ContentLength = sQuery.Length;

            //set the request timeout to 5 min.
            HttpWRequest.Timeout = 300000;
            // set the request method
            HttpWRequest.Method = "PROPPATCH";

            // You must store the data in a byte array
            byte[] ByteQuery = System.Text.Encoding.ASCII.GetBytes(sQuery);
            HttpWRequest.ContentLength = ByteQuery.Length;
            Stream QueryStream = HttpWRequest.GetRequestStream();
            // Write the data to be posted to the Request Stream

            // Send Request and Get Response
            HttpWebResponse HttpWResponse = (HttpWebResponse)HttpWRequest.GetResponse();
            // Get the Status code
            int iStatCode =  (int)HttpWResponse.StatusCode;
            string sStatus = iStatCode.ToString();
            Console.WriteLine("Status Code: {0}", sStatus);
            // Get the request headers
            string sReqHeaders = HttpWRequest.Headers.ToString();

            // Read the Response Stream
            Stream strm = HttpWResponse.GetResponseStream();
            StreamReader sr = new StreamReader(strm);
            string sText = sr.ReadToEnd();
            Console.WriteLine("Response: {0}", sText);

            // Close Stream

            // Clean Up
            myCred = null;
            myCredentialCache = null;
            HttpWRequest = null;
            HttpWResponse = null;
            QueryStream = null;
            strm = null;
            sr = null;     
         catch (Exception e)
            Console.WriteLine("{0} Exception caught.", e);

Search for TODO in the code, and then modify the code for your environment.
Build and then run the program.
Make sure that the subject of the specified item has been modified.

How to use Endpoint.publish(Object) ? has publish(Object serverContext) method. Lot of developers have questions on ...
  • qq_31230529
  • qq_31230529
  • 2015年09月14日 21:31
  • 545


本地cmd能够ping同虚拟机的IP地址,但是xshell链接时提示如下: Connecting to Could not connect to...
  • KoalaY_Doctor
  • KoalaY_Doctor
  • 2016年01月05日 10:34
  • 3247

Applications Programming in Smalltalk-80(TM):How to use Model-View-Controller (MVC)

Applications Programming in Smalltalk-80(TM):How to use Model-View-Controller (MVC) bySteve Bu...
  • mali1
  • mali1
  • 2004年07月13日 08:19
  • 1106

How to use HttpWebRequest and RegularExpressions to crash website

  • wenyuan_w
  • wenyuan_w
  • 2007年11月16日 00:22
  • 335

Fatal error: Cannot use 'Object' as class name as it is reserved in xxx/Object.php on line 77

今天闲来无事将自己本机的开发环境换成了PHP 7.2.0 Beta 3,尤其是PHP7大版本后PHP版本的更新很快。 使用yii2出现的错误在更新到PHP7.2测试版后出现的错误:Fatal erro...
  • hayixia606
  • hayixia606
  • 2017年08月26日 18:40
  • 2413

Windows Phone 7 Quick Tip: How to use Bing Maps in XNA games

 Windows Phone 7 Quick Tip: How to use Bing Maps in XNA games October 22, 2010 facebook A ...
  • minsenwu
  • minsenwu
  • 2014年12月19日 10:55
  • 736

Setting Up Transmission-Daemon To Use With Remote GUI In Ubuntu And Debian Platform : LW3xx runn...
  • openblog
  • openblog
  • 2012年05月11日 15:45
  • 2360

How to use VIM?

1. Shortcut: (1) In normal mode: Jump to line start: ^([shift] + 6) Jump to line end: $ ([shift] ...
  • fred397376230
  • fred397376230
  • 2016年01月13日 10:13
  • 156

How to log in Spring with SLF4J and Logback

In this post I will present how to log in a Spring based application with the help of Simple Logging...
  • strawbingo
  • strawbingo
  • 2015年04月14日 18:51
  • 761

How to use jquery template js

/*****by Jiangong SUN*****/ Jquery template can help generate code in the same format. It's lik...
  • garcon1986
  • garcon1986
  • 2012年04月23日 15:59
  • 3697
您举报文章:How to use the HttpWebRequest object