【发布时间】:2010-12-30 11:43:29
【问题描述】:
不久前,我为我的日常使用创建了一个小型 iphone 应用程序。现在我想在使用 C# 和 Compact Framework 时将此应用程序移植到 Windows Mobile 设备。但我真的不知道如何使用 HttpWebRequest 并且 msdn 也没有帮助我。我认为我对 Web 请求的一般工作方式理解滞后。
在 iPhone 应用程序中,我有以下行代码:
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://xxx:xxx@api.test.net/RPC2"]];
[theRequest setHTTPMethod:@"POST"];
[theRequest addValue:@"text/xml" forHTTPHeaderField:@"content-type"];
[theRequest setCachePolicy:NSURLCacheStorageNotAllowed];
[theRequest setTimeoutInterval:5.0];
NSString* pStr = [[NSString alloc] initWithFormat:@"<?xml version=\"1.0\" encoding=\"UTF-8\"?><methodCall><methodName>xxx.SessionInitiate</methodName><params><param><value><struct><member><name>LocalUri</name><value><string></string></value></member><member><name>RemoteUri</name><value><string>xxxx</string></value></member><member><name>TOS</name><value><string>text</string></value></member><member><name>Content</name><value><string>%@</string></value></member><member><name>Schedule</name><value><string></string></value></member></struct></value></param></params></methodCall>", number.text, TextView.text];
NSData* pBody = [pStr dataUsingEncoding:NSUTF8StringEncoding];
[theRequest setHTTPBody:pBody];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
Web 服务没有 wsdl,所以我必须在 .Net CF 中使用 HttpWebRquest 对象。 我没有得到的是,在我的请求中将正文(长 XML)放在哪里?
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(@"https://xxx:xxx@api.xxx.net/RPC2");
req.Method = @"POST";
req.ContentType = @"test/xml";
req.Timeout = 5;
我是这样开始的,第一行是它自己的 HttpWebRequest 并且对于 XML Body 我必须创建另一个?!如何正确使用,如何发送?对不起,如果这可能很容易,但我真的不明白。我搜索了网络、2 本书和 msdn,但在每个教程中只是一个带有 URL 但没有正文的 Web 请求。
谢谢
twickl
【问题讨论】:
标签: c# windows windows-mobile compact-framework httpwebrequest