2

我在PCL中創建了一個類來獲取所需的Session-Id和一個用於登錄到Web服務器的密鑰。 (響應是XML,它在運行Login的第二個方法中解析)我無法控制WebServer,所以我需要在客戶端解決我的問題。強制HttpWebRequest使用HTTP 1.0並在PCL中爲Windows設置UserAgent 8

這是我的工作方法:

public static async Task<string> SessionId() 
{ 
    HttpWebRequest SessiondIdRequest = (HttpWebRequest)WebRequest.Create(Constants.SessionIdUrl); 

    SessiondIdRequest.Method = "POST"; 
    SessiondIdRequest.Accept = "application/xml"; 
    SessiondIdRequest.Headers[HttpRequestHeader.AcceptLanguage] = "de-de,de;q=0.8,en-us;q=0.5,en;q=0.3"; 
    SessiondIdRequest.ContentType = "application/x-www-form-urlencoded "; 


    string appIDviaPOST = "{Here is a String}"; 
    byte[] byteArray = Encoding.UTF8.GetBytes(appIDviaPOST); 

    using (var SessionIdRequestStream = await Task<Stream>.Factory.FromAsync 
     (SessiondIdRequest.BeginGetRequestStream, SessiondIdRequest.EndGetRequestStream, SessiondIdRequest)) 
    { 
     await SessionIdRequestStream.WriteAsync(byteArray, 0, byteArray.Length); 
    } 

    WebResponse SessionIdRequestResponse = await Task<WebResponse>.Factory.FromAsync 
     (SessiondIdRequest.BeginGetResponse, SessiondIdRequest.EndGetResponse, SessiondIdRequest); 
    var SessionIdRequestResponseStream = SessionIdRequestResponse.GetResponseStream(); 
    var sr = new StreamReader(SessionIdRequestResponseStream); 
    string SessionIdResult = await sr.ReadToEndAsync(); 

    return SessionIdResult; 
} 

任務工作得非常好於WP7 & WP8。當我在我的應用程序的Windows 8部分中使用此方法時,我得到了完全不同的結果。

我用Fiddler找出兩者之間的差異,我看到WP7 & 8使用HTTP 1.0併發送UserAgent,而Win8使用HTTP 1.1並且不發送UserAgent。

在PCL中,HttpWebRequest不包含UserAgent和ProtocolVersion的屬性,所以我無法告訴Method強制它(因爲它只是一個子集)。

有什麼辦法可以解決這個問題(除了將整個方法複製到我的Win8項目中,我可以設置這些屬性)。

我對MVVM和PCL很新,所以請稍微原諒它,這對你來說是一個愚蠢的問題。 謝謝。

回答

-1

如果您無法找到User-Agent屬性,則可以嘗試設置用戶代理標頭。

SessiondIdRequest.Headers["User-Agent"] = @"Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)"; 

您也可以嘗試構造請求標頭,而不是使用提供的屬性。

Key    Value 
Request   POST http://localhost/index.aspx HTTP/1.0 
Content-Length *length* 
+0

我這樣做,它與雜項,而不是客戶端返回。另外,我認爲主要的問題是HTTP版本。非常感謝您的答案。 – MSicc 2013-03-25 06:08:12

+0

沒有工作,我想出了另一種解決方案。將盡快發佈 – MSicc 2013-03-25 20:13:11

+0

不幸的是,試圖使用'[「User-Agent」]來改變'Headers'導致這個問題:http://stackoverflow.com/q/239725/62921 – ForceMagic 2013-12-31 19:52:35

2

我通過切換到新的HttpClient的(PCL)庫和設置的版本屬性也解決了一個非常類似的問題。搜索NuGet for 'Microsoft HTTP Client Libraries'(測試截至今天,但工作得很好)。

我還是不明白,爲什麼它總是對我的作品通過使用1.0和公正零星(Win7和Win8的),使用1.1

0

時,信不信由你,不可能使用PCL何時改變的UserAgent頭。

仔細閱讀本線程的評論:

發佈Microsoft在2013年2月25日在下午4時05

Regretfully,the portable library design has been to support only properties that are globally available-- and the user agent cannot be set on all platforms (e.g., for SilverLight)

不幸的是,大部分the restricted headers都不是PCL,所以它在大多數平臺上都無法更改。

As I seen on this thread,更改這些受限標題的唯一方法是使用反射。

var httpRequest = (HttpWebRequest) WebRequest.Create(uri); 

var userAgent = httpRequest.GetType().GetProperty("UserAgent"); 
if(null != userAgent) 
{ 
    userAgent.SetValue(httpRequest, "Your value", null); 
} 

Althought,這是很不理想因爲即使它可能在某些平臺上工作,對一些人,你會得到類似這樣的錯誤:

System.InvalidOperationException:該API System.Net.HttpWebRequest.set_UserAgent(System.String)'不能在當前平臺上使用。 (看到這篇文章)

ThumbGen's answer可能是另一種選擇,如果你的項目與支持的平臺匹配,但我還沒有嘗試過。

+0

你可能想要使用帶有反射的實例標誌 – Jordan 2014-04-08 18:29:52

+0

@Martin可能會起作用,但我不認爲這是強制性的。 – ForceMagic 2014-04-21 17:32:35