1

私たちの会社は iMatrix と呼ばれる別の会社と協力しており、独自のフォームを作成するための API があります。彼らは、私たちのリクエストが彼らのサーバーにヒットしていることを確認しましたが、レスポンスはパラメーターによって決定されるいくつかの方法のいずれかで返されるはずです. 200 OK 応答が返ってきましたが、応答ヘッダーにコンテンツがなく、コンテンツの長さが 0 です。

URL は次のとおりです: https://secure4.office2office.com/designcenter/api/imx_api_call.asp

私はこのクラスを使用しています:

名前空間 WebSumit { public enum MethodType { POST = 0, GET = 1 }

public class WebSumitter
{

    public WebSumitter()
    {
    }

    public string Submit(string URL, Dictionary<string, string> Parameters, MethodType Method)
    {
        StringBuilder _Content = new StringBuilder();
        string _ParametersString = "";

        // Prepare Parameters String
        foreach (KeyValuePair<string, string> _Parameter in Parameters)
        {
            _ParametersString = _ParametersString + (_ParametersString != "" ? "&" : "") + string.Format("{0}={1}", _Parameter.Key, _Parameter.Value);
        }

        // Initialize Web Request
        HttpWebRequest _Request = (HttpWebRequest)WebRequest.Create(URL);
        // Request Method
        _Request.Method = Method == MethodType.POST ? "POST" : (Method == MethodType.GET ? "GET" : "");

        _Request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Win32)";
        // Send Request
        using (StreamWriter _Writer = new StreamWriter(_Request.GetRequestStream(), Encoding.UTF8))
        {
            _Writer.Write(_ParametersString);
        }
        // Initialize Web Response

        HttpWebResponse _Response = (HttpWebResponse)_Request.GetResponse();


        // Get Response
        using (StreamReader _Reader = new StreamReader(_Response.GetResponseStream(), Encoding.UTF8))
        {
            _Content.Append(_Reader.ReadToEnd());
        }

        return _Content.ToString();
    }

}

}

実際のパラメータはライブ システムのものであるため掲載できませんが、このコードを見て、不足しているものがないか確認していただけますか?

ありがとう!

4

2 に答える 2

2

いくつかの明らかな問題:

  • クエリ パラメータを URL エンコードしていません。値にスペースまたは特殊文字が含まれている場合、サーバーは入力を遮断したり、切り捨てたりすることがあります。
  • メソッドが GET であっても、メソッド本体でデータを送信しようとしています -- これは失敗します。GET の場合は、URL クエリ文字列に値を貼り付ける必要があります。
  • WebClientを使用するだけでなく、独自のバージョンをロールしようとしていますWebClient。以下は、WebClientパラメーターの URL エンコードを処理し、GET と POST を適切に処理するなどのサンプルです。

.

public class WebSumitter 
{ 
    public string Submit(string URL, Dictionary<string, string> Parameters, MethodType Method) 
    { 
        // Prepare Parameters String 
        var values = new System.Collections.Specialized.NameValueCollection();
        foreach (KeyValuePair<string, string> _Parameter in Parameters) 
        { 
            values.Add (_Parameter.Key, _Parameter.Value);
        } 

        WebClient wc = new WebClient();
        wc.Headers[HttpRequestHeader.UserAgent] = "Mozilla/4.0 (compatible; MSIE 6.0; Win32)"; 
        if (Method == MethodType.GET) 
        {
            UriBuilder _builder = new UriBuilder(URL);
            if (values.Count > 0) 
                _builder.Query = ToQueryString (values);
            string _stringResults = wc.DownloadString(_builder.Uri);
            return _stringResults;
        }
        else if (Method == MethodType.POST)
        {
            byte[] _byteResults = wc.UploadValues (URL, "POST", values);
            string _stringResults = Encoding.UTF8.GetString (_byteResults);
            return _stringResults;
        }
        else
        {
            throw new NotSupportedException ("Unknown HTTP Method");
        }
    }
    private string ToQueryString(System.Collections.Specialized.NameValueCollection nvc)
    {
        return "?" + string.Join("&", Array.ConvertAll(nvc.AllKeys, 
            key => string.Format("{0}={1}", System.Web.HttpUtility.UrlEncode(key), System.Web.HttpUtility.UrlEncode(nvc[key]))));
    } 
}
于 2010-05-08T00:27:57.270 に答える
1

Fiddler を使用して、応答が実際にネットワーク ワイヤを介して返されているかどうかを確認します。サーバーが空の 200 OK 応答を送信しているようです。

于 2010-05-08T00:20:48.597 に答える