【发布时间】:2017-05-05 18:14:14
【问题描述】:
我有一个我正在努力解决的附带问题,但决定在一个单独的问题中提出它,因为原始问题是冗长的,我认为这个问题的答案将解决 other question。 (一旦两个都解决了,我会更新另一个)
所以我正在 Visual Studio 2013 上执行 HttpWebRequest 并希望将 POST 添加到标题中。这工作正常,但最终服务器返回一个错误,说它不知道长度。所以从代码 sn-ps 我谷歌我应该能够只编码 request.ContentLength = byte1 其中 byte1 是头部参数的内容长度。但我的代码不会接受 .ContentLength 作为参数?为什么??
doco 说我可以,我包括使用 System.Net 和使用 System.Net.Http
那我做错了什么?
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Text;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.System;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.Web;
private async void VerifyUser()
{
loginParams = "username=" + logInUserIdString + "&password=" + logInPasswordString;
string teamResponse = "https://mySite.com/teambeta/LoginApp?" + loginParams;
Debug.WriteLine(teamResponse);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(teamResponse);
request.Method = "POST";
request.ContentType = "application/json;odata=verbose";
System.Net.HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync();
using (var sr = new StreamReader(response.GetResponseStream()))
{
logInString = sr.ReadToEnd();
}
Debug.WriteLine(logInString);
}
注意:如果我不包含 request.Method = "POST" 它会没问题,但不会返回任何值。所以我也明白默认长度是-1,但我想将其更改为 loginParams 的长度。
帮助??
【问题讨论】:
标签: c# visual-studio-2013 windows-8.1