How to use the HttpWebRequest object

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

This article was previously published under Q313126
SUMMARY
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.
MORE INFORMATION
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)
      {
         try
         {
            // 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>" +
               "</a:propertyupdate>";

            // 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
            QueryStream.Write(ByteQuery,0,ByteQuery.Length);
            QueryStream.Close();

            // 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();
            Console.WriteLine(sReqHeaders);

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

            // Close Stream
            strm.Close();

            // 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.

CocoaPods的安装使用和常见问题

转自:http://www.jianshu.com/p/6e5c0f78200a 一、什么是CocoaPods CocoaPods是iOS项目的依赖管理工具,该项目源码在Githu...
  • gf771115
  • gf771115
  • 2016年01月19日 09:30
  • 15828

appstore_被拒记录

2018年1月18日 上午3:24 发件人 Apple 5. 1.1 Legal: Privacy - Data Collection and Storage Guideline 5.1.1 - Le...
  • u012576807
  • u012576807
  • 2016年03月24日 12:45
  • 13940

计算机专外Week5-Exercises

Week 5 > Table startsWith endsWith Exercises > ProblemsQ1: Write code to print all the rows where th...
  • Herbe_chanceux
  • Herbe_chanceux
  • 2017年04月03日 16:17
  • 2086

How to use HttpWebRequest and HttpWebResponse in _NET

  • 2009年01月01日 00:50
  • 412KB
  • 下载

[win32] How to use WIN32 Event Kernel Object

【自动和手动的Event,例子很好。SM客户端的几个不同线程,有自己的Event。Event是Win32多线程同步上,最灵活的方式。】 原文链接:http://www.codeproject.com/...
  • bearcoding
  • bearcoding
  • 2016年07月15日 22:34
  • 238

How Linkers Use Static Libraries to Resolve References

While static libraries are useful and essential tools, they are also a source of confusion to progra...
  • zhangyubingcatherine
  • zhangyubingcatherine
  • 2013年12月06日 14:15
  • 442

elasticsearch install and how to use javaapi

原文链接:http://blog.csdn.net/hfmbook/article/details/51881678 Elasticsearch 基本安装 标签(空格分隔): Elasti...
  • luckgl
  • luckgl
  • 2016年11月18日 18:48
  • 411

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
  • 3683

Java ThreadLocal - How to use and code sample

Source:http://veerasundar.com/blog/2010/11/java-thread-local-how-to-use-and-code-sample/ Thread Loc...
  • j357777842
  • j357777842
  • 2011年04月01日 05:55
  • 586

How to Use HTML5 FUll Screen API(如何使用HTML5全屏接口)

此API可以使单个元素全屏显示。与按下F11键强制浏览器全屏不同,此API的目标是运行在一个容器中的图片,视频和游戏。当进入全屏模式时,将会出现一条信息通知用户可在任何时候按ESC键而返回页面。 ...
  • xiebaochun
  • xiebaochun
  • 2014年10月31日 11:39
  • 6475
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:How to use the HttpWebRequest object
举报原因:
原因补充:

(最多只允许输入30个字)