【发布时间】:2017-12-17 23:38:10
【问题描述】:
您好,我正在使用 HttpWebRequest GET 方法调用 REST 服务。我收到错误消息:- ***'Content-Type' 标头必须使用适当的属性或方法进行修改。参数名称:名称。***我从stackoverflow检查了所有与此问题相关的答案。
我的代码:-
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Button1_Click(object sender, EventArgs e)
{
Getvalue(TextBox1.Text,TextBox2.Text,TextBox3.Text);
}
private void Getvalue(string text1, string text2, string text3)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.KeepAlive = true;
request.ContentType = "appication/json";
request.Headers.Add("Content-Type", "appication/json");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string myResponse = "";
using (System.IO.StreamReader sr = new system.IO.StreamReader(response.GetResponseStream()))
{
myResponse = sr.ReadToEnd();
}
Response.Write(myResponse);
}
}
【问题讨论】:
-
那么你为什么要设置
ContentType属性然后也试图在标题中设置它?只需删除request.Headers.Add行 - 设置属性应该是您所需要的。请注意,如果您提供真实代码,它会更有用 - 您目前提供的代码不会编译。 -
你还需要 application/json,而不是 appication/json。
-
对不起..!不使用 GET 方法调用 REST 服务。使用POST方法调用rest服务
-
这有什么区别?
-
好的,乔恩我改变了你提到的代码,现在我得到了其他错误:远程服务器返回一个错误:(400)错误请求。
标签: c# wcf-data-services