2014-01-13 29 views
0

我剛剛完成了一項任務,將我的應用程序與支付網關集成在一起。所以要完成這個任務,我已經在支持網關的Windows控制檯上創建了一個示例應用程序並返回我的響應。它工作正常...我使用HttpWebRequest客戶端。Windows 8.1中的HttpWebRequest

所以,當我在Windows 8.1商店應用程序項目中添加相同的文件,它不起作用。它給我大量的錯誤(在控制檯應用程序上工作正常)。我不明白爲什麼。

我對這兩個應用程序都有相同的環境。

  1. VS 2013
  2. 的Windows 8.1

這裏是代碼的代碼片段

HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(url); 
httpRequest.Credentials = CredentialCache.DefaultCredentials; 
byte[] byteArray = Encoding.UTF8.GetBytes(request); 
httpRequest.Method = "POST";  
httpRequest.ContentLength = byteArray.Length; // **this lines gives me error(like httpRequest doesnt have ContentLength property)** 
httpRequest.ContentType = contentType; 
httpRequest.AllowAutoRedirect = false; // **same error** 
using (Stream dataStream = httpRequest.GetRequestStream()) 

回答

0

像dellywheel說的:使用HttpClient與Handler和HttpRequestMessage結合使用。

這是我的CalDAV代碼的一個例子。我只是將它從PROPFIND更改爲POST,但它仍然向您展示如何添加內容(正文),使用不同的標題等。

... 
     try { 
      HttpClientHandler httpClientHandler = new HttpClientHandler(); 
      httpClientHandler.AllowAutoRedirect = false; 
      httpClientHandler.Credentials = new NetworkCredential(caldavuserTB.Text, caldavpasswordTB.Text); 

      HttpClient httpClient = new HttpClient(httpClientHandler); 
      httpClient.MaxResponseContentBufferSize = 256000; 

      propfindMethod = new HttpMethod("POST"); 

      propfindHttpRequestMessage = new HttpRequestMessage(propfindMethod, webURLAsURI); 

      propfindHttpRequestMessage.Headers.Add("Accept", "application/xml; charset=utf-8"); 
      propfindHttpRequestMessage.Content = new StringContent("<d:propfind xmlns:d=\"DAV:\" xmlns:cs=\"http://calendarserver.org/ns/\"><d:prop><d:displayname /><cs:getctag /></d:prop></d:propfind>"); 


      propfindHttpResponseMesage = await httpClient.SendAsync(propfindHttpRequestMessage); 
     } 
    ...