发布WebRequest

ins*_*tor 2 c# windows-phone-7

我试图发布到谷歌,所以我可以登录谷歌阅读器和下载订阅列表,但我无法找到一种方法发布到Windows 7手机sdk谷歌,有没有人有一个如何做到这一点的例子?

*编辑:抱歉不是很清楚我正在尝试使用POST方法,提交电子邮件和密码谷歌登录和检索一个sid.我已经使用了WebClient和HttpWebRequest但是我看到的所有示例都发送了post数据,api调用不在windows 7 phone sdk中.

小智 18

我对您尝试使用的Google API一无所知,但如果您只需要发送POST请求,那么您当然可以使用WebClientHttpWebRequest.有WebClient,你可以使用WebClient.OpenWriteAsync()WebClient.UploadStringAsync(),文档在这里:http://msdn.microsoft.com/en-us/library/tt0f69eh%28v=VS.95%29.aspx

有了HttpWebRequest,您需要将Method属性设置为"POST".这是一个基本的例子:

var request = WebRequest.Create(new Uri(/* your_google_url */)) as HttpWebRequest;
request.Method = "POST";
request.BeginGetRequestStream(ar =>
{
    var requestStream = request.EndGetRequestStream(ar);
    using (var sw = new StreamWriter(requestStream))
    {
        // Write the body of your request here
    }

    request.BeginGetResponse(a =>
    {
        var response = request.EndGetResponse(a);
        var responseStream = response.GetResponseStream();
        using (var sr = new StreamReader(responseStream))
        {
            // Parse the response message here
        }

    }, null);

}, null);
Run Code Online (Sandbox Code Playgroud)

WebClient班可能会更易于使用,但较少定制.例如,我没有看到能够将cookie附加到WebClient请求的方法,或者在使用时设置Content-Type标头的方法WebClient.