【发布时间】:2015-06-23 09:34:39
【问题描述】:
我得到了一些奇怪的不一致结果。这种网络方法早些时候工作得很好。我只添加了一些 UI 内容(jquery-ui 和 twitter-bootstrap)。我尝试删除所有 UI 内容,但仍然出现此错误。我不确定 JIRA 服务器是否有问题,或者我没有正确创建请求。
我已尝试执行以下操作 (based on this):
- 使用
System.Web.HttpUtility.HtmlDecode textVar = textVar.Replace((char)(0x1F), ' ').Trim();
但是,两者都没有奏效。
这段代码没有抛出异常,但他在 C# 代码中的var response 有这个错误堆栈跟踪。输出:
RestSharp response status: Error -
HTTP response: Forbidden -
Forbidden -
'', hexadecimal value 0x1F, is an invalid character. Line 1, position 1. -
System.Xml.XmlException: '', hexadecimal value 0x1F, is an invalid character. Line 1, position 1.
at System.Xml.XmlTextReaderImpl.Throw(Exception e)
at System.Xml.XmlTextReaderImpl.Throw(String res, String[] args)
at System.Xml.XmlTextReaderImpl.ThrowInvalidChar(Char[] data, Int32 length, Int32 invCharPos)
at System.Xml.XmlTextReaderImpl.ParseRootLevelWhitespace()
at System.Xml.XmlTextReaderImpl.ParseDocumentContent()
at System.Xml.XmlTextReaderImpl.Read()
at System.Xml.Linq.XDocument.Load(XmlReader reader, LoadOptions options)
at System.Xml.Linq.XDocument.Parse(String text, LoadOptions options)
at System.Xml.Linq.XDocument.Parse(String text)
at RestSharp.Deserializers.XmlDeserializer.Deserialize[T](IRestResponse response)
at RestSharp.RestClient.Deserialize[T](IRestRequest request, IRestResponse raw) -
空引号'' 实际上是其他一些类似于:[US] 的字符。我可以做些什么来解决这个问题,或者这似乎是一个 JIRA 严重问题?
我的最小化示例:
private static string serverUrl = "https://www.url.com/jira";
private static string baseUrl = "/rest/api/2/";
private static string searchCommand = "search";
public static void QuickTest()
{
string username = HttpContext.Current.Session["jira_username"].ToString();
string pw = HttpContext.Current.Session["jira_password"].ToString();
//also tried hardcoding these values. didn't work either
//username = "";
//pw = "";
//create client
RestClient client = new RestClient(serverUrl)
{
Authenticator = new HttpBasicAuthenticator(username, pw)
};
//create query
string jql = "assignee=msalim";
//string jql = "assignee%3Dmsalim"; //didn't work either
//create request
var request = new RestRequest();
request.Resource = Path.Combine(baseUrl, searchCommand);
request.AddParameter(new RestSharp.Parameter()
{
Name = "jql",
Value = jql,
Type = ParameterType.GetOrPost
});
request.Method = Method.GET;
//get response
var response = client.Execute<Issues>(request);
string errorMessage = string.Format("RestSharp response status: {0} -\n HTTP response: {1} -\n {2} -\n {3} -\n {4} -\n ", response.ResponseStatus, response.StatusCode, response.StatusDescription, response.ErrorMessage, response.ErrorException);
System.Diagnostics.Debug.WriteLine(errorMessage);
}
另外,我已经尝试在我的 Firefox 浏览器中输入这两个,它们都返回了预期的结果:
https://www.url.com/jira/rest/api/2/search?jql=assignee%3Dmsalim
https://www.url.com/jira/rest/api/2/search?jql=assignee=msalim
【问题讨论】:
-
您需要查看实际响应以找出问题所在 - 即使用 Fiddler 捕获...请注意,您也应该能够从命令行应用程序执行此操作,并且不需要任何您帖子中的 JavaScript(您也可以简化 C# 示例以使用常量 Url)。
-
@AlexeiLevenkov 我已经简化了代码。您的意思是在执行后检查响应的请求吗?
-
不是直接反序列化为
Issues对象,而是尝试client.Execute(request)来查看原始内容?还是那是您之前进行替换的地方? -
@pilotcam 这只是给了我同样的错误信息。
Content始终是不可读的字符串。 -
顺便说一句,您不想使用 Url.Combine 代替 Path.Combine 吗?您的请求路径是否可能无效?
标签: c# jquery ajax restsharp jql