5

C# から HTTP Post リクエストを作成したいと考えています。このリクエストにはカスタム ヘッダーがあります。プログラムを開始しようとすると、次の例外が発生しました。

イタリアの:

Questa intestazione deve essere modificata utilizzando la proprietà o il method appropriato. 名前パラメータ: 名前

英語:

このヘッダーは、適切なプロパティまたはメソッドを使用して変更する必要があります。

オンライン:request.Headers.Add("Content-Type", "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();
         }
    }
}

4

2 に答える 2

14

WebRequest.ContentType プロパティを使用します。一部のヘッダーは、API プロパティのみを使用して設定できます。

編集:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.androidlost.com/androidlost/greet");
request.ContentType = "text/x-gwt-rpc; charset=utf-8";
于 2012-06-21T15:13:54.910 に答える
3

MSDN のドキュメントによると、HttpWebRequest.Header Property .

Content-Type は、ContentTypeプロパティを使用して変更されます。WebRequestこれには、にキャストする必要がありますHttpWebRequest

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.androidlost.com/androidlost/greet");
于 2012-06-21T15:11:33.787 に答える