How to use the HttpWebRequest object
来源:互联网 发布:ug编程出来工资多少 编辑:程序博客网 时间:2024/05/18 02:45
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.
- How to use the HttpWebRequest object
- How to use HttpWebRequest and RegularExpressions to crash website
- How to use the CAknWaitDialog
- how to use the CAknwaitDialog
- How to use the CAknWaitDialog
- How to use the BAPI_BUPA_ADDRESS_CHANGE
- how to use javascript Object.defineProperty?
- How to use Endpoint.publish(Object) ?
- How to use HttpWebRequest to send POST request to another web server?
- How to use HttpWebRequest to post data to another page which is on another server
- How to use HttpWebRequest to send POST request to another web server?
- How To Use the C Run-Time
- How to Use the Visual C++ DEBUGGER
- How to Create and Use the DLL
- How to use the RTC driver
- How to use the CONVERSION_EXIT_PARVW_INPUT and CONVERSION_EXIT_PARVW_OUTPUT
- the examples :how to use LP_SOLVE.EXE
- HOW TO Use the GCC specs file
- 左宗棠:大清朝最后一棵顶梁柱
- 三层结构的总结
- 7月7日.没齿难忘啊.
- first article
- 涂鸦
- How to use the HttpWebRequest object
- 哈哈。。没想到现在到处都是blog..
- MX2004 组件(一):焦点管理器(FocusManager)
- MX2004组件(二)Accordion[折叠菜单]
- [ASP.NET] Session 详解
- C++從零開始
- 语义的需要
- MX2004组件(三):DataHolder
- 学习心得---关于seh(1)