2014-01-13 27 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); 
     } 
    ...