2010-05-08 135 views
1

我們公司與另一家名爲iMatrix的公司合作,他們有一個用於創建我們自己的表單的API。他們已經確認我們的請求正在觸及他們的服務器,但是響應應該以一種由參數確定的幾種方式返回。我收到了200 OK響應,但響應頭中沒有內容,內容長度爲0。HTTPWebResponse不返回任何內容

這裏是網址: https://secure4.office2office.com/designcenter/api/imx_api_call.asp

我使用這個類:

命名空間WebSumit { 公共枚舉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(); 
    } 

} 

}

我無法發佈實際參數,因爲它們是針對實時系統的,但是您能否查看此代碼並查看是否有任何遺漏?

謝謝!

回答

1

使用Fiddler查看是否有任何響應實際上通過網絡線返回。這聽起來像服務器發送一個空的200 OK響應。

+0

http://www.fiddler2.com/fiddler2/ – dthorpe 2010-05-08 00:21:58

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])))); 
    } 
} 
相關問題