C# System.FormatException在Windows phone 8中添加用户代理时发生异常

C# System.FormatException在Windows phone 8中添加用户代理时发生异常,c#,windows-phone-8,httpclient,C#,Windows Phone 8,Httpclient,我想在windows phone 8应用程序中使用以下用户代理 string userAgentString = "Mozilla/5.0 (compatible; MSIE 10.0; Windows Phone " + Environment.OSVersion.Version.ToString(); userAgentString += "; Trident/6.0; IEMobile/10.0; ARM; Touch; " + Microsoft.Phone.Info.DeviceSta

我想在windows phone 8应用程序中使用以下用户代理

string userAgentString = "Mozilla/5.0 (compatible; MSIE 10.0; Windows Phone " + Environment.OSVersion.Version.ToString();
userAgentString += "; Trident/6.0; IEMobile/10.0; ARM; Touch; " + Microsoft.Phone.Info.DeviceStatus.DeviceManufacturer + ";";
userAgentString += Microsoft.Phone.Info.DeviceStatus.DeviceName;
httpClient.DefaultRequestHeaders.UserAgent.ParseAdd(userAgentString);
但是我得到一个System.FormatException,如何正确格式化用户代理字符串。假定字符串的形式如下:

Mozilla/5.0 (compatible; MSIE 10.0; Windows Phone 8.0; Trident/6.0; IEMobile/10.0; ARM; Touch; NOKIA; Lumia 920)  
这是个例外

A first chance exception of type 'System.FormatException' occurred in System.Net.Http.DLL
   at System.Net.Http.Headers.HttpHeaderParser.ParseValue(String value, Object storeValue, Int32& index)
   at System.Net.Http.Headers.HttpHeaders.ParseAndAddValue(String name, HeaderStoreItemInfo info, String value)
   at System.Net.Http.Headers.HttpHeaders.Add(String name, String value)
   at System.Net.Http.Headers.HttpHeaderValueCollection`1.ParseAdd(String input)
   at MyApp.Assets.Class.MyAppNetUtils.<GetRemoteStringContent>d__0.MoveNext()
System.Net.Http.DLL中发生了类型为“System.FormatException”的首次意外异常
位于System.Net.Http.Headers.HttpHeaderParser.ParseValue(字符串值、对象存储值、Int32和索引)
位于System.Net.Http.Headers.HttpHeaders.ParseAndAddValue(字符串名称、HeaderStoreItemInfo信息、字符串值)
在System.Net.Http.Headers.HttpHeaders.Add(字符串名称,字符串值)
位于System.Net.Http.Headers.HttpHeaderValueCollection`1.ParseAdd(字符串输入)
在MyApp.Assets.Class.MyAppNetUtils.d_u0.MoveNext()中

您可以使用php函数,也可以使用此函数查找您的用户代理

$user_agent = $_SERVER['HTTP_USER_AGENT'];
echo $user_agent;

在您的windows mobile中进行测试。

在哪一行出现此异常?堆栈跟踪到底是什么?异常显示了什么?我在userAgentString中看不到右括号。也许这就是问题的原因。顺便说一句,您可以使用String.Format来避免此类问题;对我来说,String.Format更具描述性。@ViktorArsanov你说得对,添加了右括号并澄清了这个问题。感谢您还可以使用引导来避免从头开始的错误