ObexWebRequest Class

Provides an OBEX implementation of the WebRequest class.

Definition

Namespace: InTheHand.Net
Assembly: InTheHand.Net.Obex (in InTheHand.Net.Obex.dll) Version: 4.0.5
C#
public class ObexWebRequest : WebRequest
Inheritance
WebRequest    ObexWebRequest

Remarks

If you want to transfer a file or other object using the standard service as used by Windows' Wireless Link / Bluetooth File Transfer Wizard, Palm's Beam, Nokia's Send via Infrared, then use the OBEX protocol.

The PUT operation is supported, and there is new support for GET, (see the documentation at the Method property). Changing folders is not supported, nor is getting a folder listing.

In the previous version there were some issue with handling file names that include non-English characters, and connections to some device types failed. Also if the connection to the peer was lost then the request could hang reading forever. See the release note and bugs database for more information.

Example

For Bluetooth one can use code like the following to send a file: (Note a failure is signalled by an exception).
VB
Dim addr As BluetoothAddress = BluetoothAddress.Parse("002233445566")
Dim path As String = "HelloWorld.txt"
'
Dim req As New ObexWebRequest(addr, path)
req.ReadFile("Hello World.txt")
Dim rsp As ObexWebResponse = CType(req.GetResponse(),ObexWebResponse)
Console.WriteLine("Response Code: {0} (0x{0:X})", rsp.StatusCode)
That constructor isn't available for other transports (TCP/IP, IrDA) so one has to create a Uri to provide the scheme, address, and path parameters. Thus use code like the following to send a file.
VB
' The host part of the URI is the device address, e.g. IrDAAddress.ToString(),
' and the file part is the OBEX object name.
Dim addr As BluetoothAddress = ...
Dim addrStr As String = addr.ToString("N")
Dim uri As New Uri("obex://" & addrStr & "/HelloWorld.txt")
'
Dim req As New ObexWebRequest(uri)
req.ReadFile("Hello World.txt")
Dim rsp As ObexWebResponse = CType(req.GetResponse(),ObexWebResponse)
Console.WriteLine("Response Code: {0} (0x{0:X})", rsp.StatusCode)
Or, to send locally generated content use something like the following.
VB
Dim addr As BluetoothAddress = ...
Dim path As String = "HelloWorld2.txt"
'
Dim req As New ObexWebRequest(addr, path)
Using content As Stream = req.GetRequestStream()
   ' Using a StreamWriter to write text to the stream...
   Using wtr As New StreamWriter(content)
      wtr.WriteLine("Hello World GetRequestStream")
      wtr.WriteLine("Hello World GetRequestStream 2")
      wtr.Flush()
      ' Set the Length header value
      req.ContentLength = content.Length
   End Using
   ' In this case closing the StreamWriter also closed the Stream, but ...
End Using
Dim rsp As ObexWebResponse = CType(req.GetResponse(),ObexWebResponse) 
Console.WriteLine("Response Code: {0} (0x{0:X})", rsp.StatusCode)
See also the ObexPushApplication and ObexPushVB sample programs.

Constructors

ObexWebRequest(Uri) Create a new Obex request with the specified Uri.
ObexWebRequest(BluetoothAddress, String) Initialize an instance of this class given a Bluetooth Device Address, and a remote path name.
ObexWebRequest(Uri, Stream) [Advanced usage] Create a new Obex request with the specified Uri and the open Stream connection to an OBEX server.
ObexWebRequest(String, BluetoothAddress, String) Initialize an instance of this class given a scheme, a Bluetooth Device Address, and a remote path name.

Properties

ContentLength Gets or sets the Length OBEX header.
ContentType Gets or sets the value of the Type OBEX header.
Headers Specifies a collection of the name/value pairs that make up the OBEX headers.
Method Gets or sets the method for the request.
Proxy Not Supported - do not use, this will throw an exception.
RequestUri Gets the original Uniform Resource Identifier (URI) of the request.
Timeout Gets or sets the time-out value for the GetResponse method.

Methods

BeginGetResponse Begins a request for a OBEX server response.
EndGetResponse Begins a request for a OBEX server response.
GetRequestStream Gets a Stream object to use to write request data.
GetResponse Returns the OBEX server response.
GetResponseAsync 
ReadFile Reads the contents of the specified file to the request stream.

See Also