2014-05-19 108 views
1

我花了一整天的時間來解決這個問題。我有一個自定義的網絡服務器,並請求從Chrome或POSTman ReST客戶端工作正常。一旦我在c#中使用webclient或httpwebrequest,我就會得到:服務器違反協議。 Section = ResponseStatusLine嘗試將zip文件傳輸到客戶端時。服務器違反了協議。 Section = ResponseStatusLine in c#

我曾嘗試:

public static bool SetAllowUnsafeHeaderParsing20() 
{ 
    //Get the assembly that contains the internal class 
    Assembly aNetAssembly = Assembly.GetAssembly(typeof(System.Net.Configuration.SettingsSection)); 
    if (aNetAssembly != null) 
    { 
     //Use the assembly in order to get the internal type for the internal class 
     Type aSettingsType = aNetAssembly.GetType("System.Net.Configuration.SettingsSectionInternal"); 
     if (aSettingsType != null) 
     { 
      //Use the internal static property to get an instance of the internal settings class. 
      //If the static instance isn't created allready the property will create it for us. 
      object anInstance = aSettingsType.InvokeMember("Section", BindingFlags.Static | BindingFlags.GetProperty | BindingFlags.NonPublic, null, null, new object[] { }); 
      if (anInstance != null) 
      { 
       //Locate the private bool field that tells the framework is unsafe header parsing should be allowed or not 
       FieldInfo aUseUnsafeHeaderParsing = aSettingsType.GetField("useUnsafeHeaderParsing", BindingFlags.NonPublic | BindingFlags.Instance); 
       if (aUseUnsafeHeaderParsing != null) 
       { 
        aUseUnsafeHeaderParsing.SetValue(anInstance, true); 
        return true; 
       } 
      } 
     } 
    } 
    return false; 
} 

<system.net> 
    <settings> 
    <httpWebRequest useUnsafeHeaderParsing="true" /> 
    </settings> 
</system.net> 
中的app.config

我也試圖保持存活=虛假和混亂與頭太/

這是WebRequest的,其失敗的client2Downloadfile電話:

private void sendManifest() 
{ 

    Uri remoteuri = new Uri(Properties.Settings.Default.masterurl); 

    SetAllowUnsafeHeaderParsing20(); 
    using (WebClient client = new WebClient()) 
    { 
     NameValueCollection reqparm = new NameValueCollection(); 
     reqparm.Add("application", "TestApp"); 
     reqparm.Add("manifest", manifest); 
     try 
     { 
      byte[] responsebytes = client.UploadValues(Properties.Settings.Default.masterurl, "POST", reqparm); 
      string responsebody = Encoding.UTF8.GetString(responsebytes); 
      if (responsebody != "") 
      { 
       using (WebClient client2 = new WebClient()) 
       { 
        client2.DownloadFile(Properties.Settings.Default.masterurl + "//" + responsebody + "transfer.zip", "c:\\temp.zip"); 
       } 
      } 
     } 
     catch (Exception e) 
     { 
      Console.WriteLine(e.ToString()); 
     } 
    } 
} 

的Web服務器響應可以看出:

http://gplus2.net:9532/97e456f0-9b57-4315-b03d-40a67f76d440/transfer.zip 

任何援助是非常感謝,因爲我已經字面上用完了想法。這顯然是一個格式錯誤的服務器標題,但我保持在最低限度。

回答

4

我有同樣的問題,我用下面的方法解決它。我創建了一個覆蓋GetWebRequestMethod的自定義Web客戶端。

class CustomWebClient : WebClient 
{ 
    /// <summary> 
    /// Returns a <see cref="T:System.Net.WebRequest" /> object for the specified resource. 
    /// </summary> 
    /// <param name="address">A <see cref="T:System.Uri" /> that identifies the resource to request.</param> 
    /// <returns> 
    /// A new <see cref="T:System.Net.WebRequest" /> object for the specified resource. 
    /// </returns> 
    protected override WebRequest GetWebRequest(Uri address) 
    { 
     WebRequest request = base.GetWebRequest(address); 
     if (request is HttpWebRequest) 
     { 
      (request as HttpWebRequest).KeepAlive = false; 
     } 
     return request; 
    } 
} 

然後我以正常的方式提出的要求,這樣的

using (CustomWebClient client = new CustomWebClient()) 
     { 
      client.Headers[HttpRequestHeader.Authorization] = "Basic " + base64String; 
      responseData = client.DownloadData(baseUri); 
     } 
0

我這個問題掙扎了很久,最終的問題是,我加入到IIS中的自定義標題.. 。我從某處複製了該值,它包含{cr}:

打開IIS管理器 - >進入網站 - >在內容視圖上,選擇「Response Headers」。

檢查IIS上的自定義標題,如果不需要它們,則刪除標題,並查看標題是否具有格式錯誤的值。

相關問題