C# 具有自定义标头的HTTP Post请求
我想从C#发出HTTP Post请求。此请求具有自定义标题。当我尝试启动程序时,收到以下异常: 意大利语: 发展中的知识产权改革 我是适当的。Nome参数:名称 中文: 必须使用适当的属性或方法修改此标头 在线:C# 具有自定义标头的HTTP Post请求,c#,http-headers,C#,Http Headers,我想从C#发出HTTP Post请求。此请求具有自定义标题。当我尝试启动程序时,收到以下异常: 意大利语: 发展中的知识产权改革 我是适当的。Nome参数:名称 中文: 必须使用适当的属性或方法修改此标头 在线:request.Headers.Add(“内容类型”,“text/x-gwt-rpc;charset=utf-8”) 这是我的代码: using System; using System.Collections.Generic; using System.Linq; using Syst
request.Headers.Add(“内容类型”,“text/x-gwt-rpc;charset=utf-8”)代码>
这是我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Collections.Specialized;
using System.IO;
using System.Text.RegularExpressions;
using System.Dynamic;
using System.Collections;
using System.Collections.ObjectModel;
using System.Net.Security;
using System.Web;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
WebRequest request = WebRequest.Create("http://www.androidlost.com/androidlost/greet");
request.Method = "POST";
request.Headers.Add("Content-Type", "text/x-gwt-rpc; charset=utf-8");
string postData = "Test";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
}
}
}
根据MSDN文档
使用ContentType
属性修改内容类型。
这要求您将WebRequest
强制转换为HttpWebRequest
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.androidlost.com/androidlost/greet");
使用WebRequest.ContentType属性。某些标头只能使用API属性设置
编辑:
谢谢安迪。。。请给我写一段代码来解决这个问题。。。当做
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.androidlost.com/androidlost/greet");
request.ContentType = "text/x-gwt-rpc; charset=utf-8";