Server.UrlEncode和HttpUtility.UrlEncode之间有区别吗?
我创建了一个C#库项目.该项目在一个类中有这一行:
JsonConvert.SerializeObject(objectList);
Run Code Online (Sandbox Code Playgroud)
我收到错误说
名称JsonConvert在当前上下文中不存在.
为了解决这个问题,我添加System.ServiceModel.Web.dll了参考但没有运气.我该如何解决这个错误?
这是我第一次曾经使用JSON,甚至System.Net和WebRequest部分在我的任何应用程序.我的应用程序应该发送一个JSON有效负载,类似于下面的一个到身份验证服务器:
{
"agent": {
"name": "Agent Name",
"version": 1
},
"username": "Username",
"password": "User Password",
"token": "xxxxxx"
}
Run Code Online (Sandbox Code Playgroud)
为了创建这个有效载荷,我使用了JSON.NET库.我如何将此数据发送到身份验证服务器并接收其JSON响应?以下是我在一些示例中看到的内容,但没有JSON内容:
var http = (HttpWebRequest)WebRequest.Create(new Uri(baseUrl));
http.Accept = "application/json";
http.ContentType = "application/json";
http.Method = "POST";
string parsedContent = "Parsed JSON Content needs to go here";
ASCIIEncoding encoding = new ASCIIEncoding();
Byte[] bytes = encoding.GetBytes(parsedContent);
Stream newStream = http.GetRequestStream();
newStream.Write(bytes, 0, bytes.Length);
newStream.Close();
var response = http.GetResponse();
var stream = response.GetResponseStream();
var sr = new StreamReader(stream);
var …Run Code Online (Sandbox Code Playgroud) Uri类默认为RFC 2396.对于OpenID和OAuth,我需要符合RFC 3986的Uri转义.
默认情况下,URI中的任何保留字符都按照RFC 2396进行转义.如果启用了国际资源标识符或国际域名解析,则此行为会发生变化,在这种情况下,URI中的保留字符将根据RFC 3986和RFC 3987进行转义.
该文档还指出激活此IRI模式以及RFC 3986行为意味着将一个uri节元素添加到machine.config并将其添加到app/web.config文件中:
<configuration>
<uri>
<idn enabled="All" />
<iriParsing enabled="true" />
</uri>
</configuration>
Run Code Online (Sandbox Code Playgroud)
但是,无论是否存在于.config文件中,我都会获得与.NET 3.5 SP1应用程序相同的(非3986)转义行为. 我还需要做些什么Uri.EscapeDataString才能使用RFC 3986规则?(具体来说,要转义RFC中定义的保留字符)
我有一个URL我希望以data ="blahblahblah"的形式发布带有参数的主体.但是,在这种情况下,我的"blahblahblah"是一个完整的XML,我将其简化为如下所示:
<Parent id="1">
<Child id="1"/>
</Parent>
Run Code Online (Sandbox Code Playgroud)
我可以通过以下方法找到HTTPClient FormUrlEncodedContent查找工作.
var values = new List<KeyValuePair<string, string>>();
values.Add(new KeyValuePair<string, string>("data", XMLBody));
var content = new FormUrlEncodedContent(values);
HttpResponseMessage sResponse = await sClient.PostAsync(action.URL, content).ConfigureAwait(false);
Run Code Online (Sandbox Code Playgroud)
现在我想让它与StringContent一起使用. 基本上发送xml作为参数值的一部分,并且xml包含"=".下面的代码不起作用,因为我可以发布它,但服务器无法识别xml数据.我在这里做错了吗?
StringContent content = new StringContent(HttpUtility.UrlEncode(action.Body), Encoding.UTF8, "application/x-www-form-urlencoded");
HttpResponseMessage sResponse = await sClient.PostAsync(action.URL, content ).ConfigureAwait(false);
Run Code Online (Sandbox Code Playgroud) 以下代码用于通过 pardot api 发送电子邮件。
if (ConfigurationManager.AppSettings.Count > 0)
{
uri = ConfigurationManager.AppSettings["PardotURI"].ToString() + "email/version/4/do/send/prospect_email/" + email;
uri += "?user_key=" + ConfigurationManager.AppSettings["PardotUserKey"].ToString();
uri += "&api_key=" + GetAPIKey() + "&campaign_id=" + GetPardotCampaign("Capis News");
uri += "&from_email=" + ConfigurationManager.AppSettings["FromEmail"].ToString();
uri += "&from_name=" + ConfigurationManager.AppSettings["FromName"].ToString();
uri += "&name=FlyNews - " + DateTime.Now.ToString("MM/dd/yyy h:mm tt");
uri += "&subject=CAPIS: Client Holdings News " + DateTime.Today.ToString("MM/dd/yyyy");
}
try
{
MultipartFormDataContent data = new MultipartFormDataContent();
data.Add(new StringContent(htmlContent), "html_content");
data.Add(new StringContent(textContent), "text_content");
await client.PostAsync(uri, data);
client.Dispose();
}
catch(Exception ex) …Run Code Online (Sandbox Code Playgroud)