private void CreateNewTasklist(string TasklistTitleValue)
{
AuthenticationProcess objUri = new AuthenticationProcess();
if (string.IsNullOrEmpty(TasklistTitleValue))
{
MessageBox.Show("Please Enter the Tasklist Title.");
}
else
{
//Read the Token result in Isolated storage.
string TokenResult = TasklistManager.ReadTokenResult();
var Token = Auth.ParseAuthJsonString(TokenResult);
var request = new RestRequest(objUri.CreateTasklist, Method.POST);
request.AddHeader("content-type", "application/json");
RestClient client = new RestClient("https://www.googleapis.com");
client.Authenticator = new OAuth2AuthorizationRequestHeaderAuthenticator(Token.access_token);
client.ExecuteAsync<TasklistManager>(request, TaskListsLoaded);
}
}
这里我得到字符串TasklistTitleValue值..我想发布它。如何将内容类型添加到请求主体是application / json。
答案 0 :(得分:0)
你可以这样做:
var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://url");
httpWebRequest.ContentType = "text/json";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = "{\"user\":\"test\"," +
"\"password\":\"bla"}";
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
}
或者你可以使用它: