windows 在紧凑框架项目上需要有关 HttpWebRequest 的帮助

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1969608/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 13:39:32  来源:igfitidea点击:

Need help with HttpWebRequest on a Compact Framework Project

c#windowswindows-mobilecompact-frameworkhttpwebrequest

提问by Jerther

not so long ago I′ve created a small iphone app for my Daily use. Now I want to port this app to a Windows Mobile Device while using C# and the Compact Framework. But I really have no clue how to use the HttpWebRequest and the msdn doesn′t help me either. I think I have a lag of understanding on how Web Requests work in general.

不久前,我创建了一个小 iphone 应用程序供我日常使用。现在我想在使用 C# 和 Compact Framework 的同时将此应用程序移植到 Windows Mobile 设备。但我真的不知道如何使用 HttpWebRequest 并且 msdn 也没有帮助我。我认为我对 Web 请求的一般工作方式理解滞后。

In the iPhone app I have the following lines code:

在 iPhone 应用程序中,我有以下几行代码:

NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://xxx:[email protected]/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];

The Webservice has no wsdl so I have to use the HttpWebRquest Object in .Net CF. What I didn′t get is, where to put the Body (the long XML) in my Request?

Webservice 没有 wsdl,所以我必须在 .Net CF 中使用 HttpWebRquest 对象。我没有得到的是,在我的请求中将正文(长 XML)放在哪里?

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(@"https://xxx:[email protected]/RPC2");
req.Method = @"POST";
req.ContentType = @"test/xml";
req.Timeout = 5;

I started this way, is the first line it′s own HttpWebRequest and for the XML Body I have to create anotherone?! How do I use it correctly, how do I send it? Sorry if this might be normaly totaly easy but I really don′t get it. I′ve searched the web, 2 Books and the msdn but in every tutorial is only a Webrequest with an URL but without a body.

我是这样开始的,第一行是它自己的 HttpWebRequest 并且对于 XML Body 我必须创建另一个?!如何正确使用,如何发送?对不起,如果这可能很容易,但我真的不明白。我在网上搜索了 2 本书和 msdn,但在每个教程中都只有一个带有 URL 但没有正文的 Web 请求。

Thank you

谢谢

twickl

点点滴滴

回答by Darin Dimitrov

You need to write the POST data to the request stream.

您需要将 POST 数据写入请求流

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(@"https://username:[email protected]/RPC2");
req.Method = "POST";
req.ContentType = "test/xml";
req.Timeout = 5;
using (Stream stream = req.GetRequestStream())
using (StreamWriter writer = new StreamWriter(stream, Encoding.UTF8))
{
    writer.Write("PUT THE XML HERE");
}

using (StreamReader reader = req.GetResponse().GetResponseStream())
{
    string result = reader.ReadToEnd();
}

回答by Jerther

BEWARE, if there's no data to write to the stream, the ContentLengthwould be 0 but oddly you STILL need to Dispose()the RequestStream even if you don't write anything to it!!!

请注意,如果没有要写入流的数据,则该ContentLength值为 0,但奇怪的是Dispose(),即使您不向其中写入任何内容,您仍然需要使用 RequestStream !!!

req.ContentLength = 0;
req.GetRequestStream().Dispose();

This problem only happens in Compact Framework.

这个问题只发生在 Compact Framework 中。

I want my 8 hours back, and my hair too...

我想要我的 8 小时回来,我的头发也是...

回答by dnewcome

Get the request stream using

使用获取请求流

Stream requestStream = req.GetRequestStream();

Then write your xml data to the stream, taking care to encode your text.

然后将您的 xml 数据写入流,注意对您的文本进行编码。

Don't forget to close the stream to ensure that all of your data is sent.

不要忘记关闭流以确保发送所有数据。

requestStream.Close();