如何指定的Web客户端邮件正文? [英] How to specify message body in a WebClient?

查看:781
本文介绍了如何指定的Web客户端邮件正文?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在控制台中,我跟进呼叫的站点,我上正在,我可以看到的地址( some.site.com/gettoken 的),消息头和一些FF来电的邮件正文的。这是在后者,我可以看到我正在发送在网站上输入的凭据。

In the console, I follow up a call the site I'm on is is making and I can see the address (some.site.com/gettoken), message header and something that FF calls Message Body. It's in the latter that I can see the credentials that I've entered on the site that are being sent.

所以,我已经得到了网址和邮件正文。于是,我试着用C#像这样我的的Azure服务层实现的行为。

So, I've got the URL and the message body. Then, I've tried to implement the behavior using C# for my Azure service layer like so.

String url = @"https://some.site.com/gettoken";
String credentials = "username=super&password=secret";
using (WebClient client = new WebClient())
{
  String output = client.UploadString(url, credentials);
  result = output;
}

不过,我得到错误400 - 坏的结果。我错过了什么?

However, I get error 400 - bad result. What did I miss?

我GOOGLE了<一个href=\"https://www.google.se/search?newwindow=1&site=&source=hp&q=how+to+specify+%22message+body%22+with+webclient+in+c%23&oq=how+to+specify+%22message+body%22+with+webclient+in+c%23&gs_l=hp.3...1766.16798.0.17267.59.56.1.1.1.1.235.6256.15j35j3.53.0.msedrc...0...1c.1.58.hp..22.37.3951.0.HXF6Ssd-Qz0\"相对=nofollow>一些东西但唯一的远程相关命中谈论上传方法,我已经使用。难道我完全找错了树或只是失去了一些东西的小? 一些人似乎得到它的工作,但他们没有标记化身边。而且我还不够某些以确定它是否是相关的或没有。

I've googled for some stuff but the only remotely relevant hits are talking about the upload methods, which I've used. Am I barking up the wrong tree entirely or just missing something tiny? Some people seem to get it to work but they're not tokenizing around. And I'm not certain enough to determine whether it's of relevance or not.

推荐答案

那么,作为什么在评论中讨论的总结:你可以使用<一个href=\"http://stackoverflow.com/questions/20530152/need-help-deciding-between-httpclient-and-webclient\">the更现代 的HttpClient 来代替。

So, as a summary of what has been discussed in the comments: you can use the more modern HttpClient instead.

请注意,这是的 System.Net.Http.HttpClient 并没有的 Windows.Web.Http.HttpClient

Note that this is the System.Net.Http.HttpClient and not Windows.Web.Http.HttpClient.

这是示例实现可能看起来像这样:

An example implementation could look like this:

public async Task<string> SendCredentials()
{
    string url = @"https://some.site.com/gettoken";
    string credentials = "username=super&password=secret";
    using(var client = new HttpClient())
    {
        var response = await client.PostAsync(url, new StringContent(credentials));
        return await response.Content.ReadAsStringAsync();
    }
}

您可能也有兴趣的 System.Net.Http.FormUrlEn codedContent 它允许你在参数和它们的值传递所以你不'吨有构造的凭据珍惜自己。

You might also be interested in System.Net.Http.FormUrlEncodedContent which allows you to pass in the parameters and their values so you don't have to construct the credentials value yourself.

更多信息异步/的await

这篇关于如何指定的Web客户端邮件正文?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆