我试图让我的脑袋缠绕在Windows Phone 7的restsharp上.我正在尝试使用POST方法将一些数据提供给服务器,但是当它到达client.executeasync行时,它崩溃并出现错误"无效的URI" :URI Scheme无效." 我错过了什么?
var client = new RestSharp.RestClient("10.0.1.20:8085");
var request = new RestSharp.RestRequest("/test", RestSharp.Method.POST);
request.AddParameter("param1", "value1", RestSharp.ParameterType.GetOrPost);
request.AddParameter("param2", "value2", RestSharp.ParameterType.GetOrPost);
request.AddParameter("param3", "value3", RestSharp.ParameterType.GetOrPost);
request.AddParameter("param4", "value4", RestSharp.ParameterType.GetOrPost);
client.ExecuteAsync(request, (response) =>
{
var auth = response.Content;
});
Run Code Online (Sandbox Code Playgroud) 我有这个代码,我试图转换为RestSharp.为了清楚起见,我删除了使用块来压缩它.
using System.IO;
using System.Net;
using RestSharp;
string GetResponse(string url,string data)
{
var request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
var bytes = Encoding.UTF8.GetBytes(data);
request.ContentLength = bytes.Length;
request.GetRequestStream().Write(bytes, 0, bytes.Length);
var response = (HttpWebResponse)request.GetResponse();
var stream = response.GetResponseStream();
if (stream == null) return string.Empty;
var reader = new StreamReader(stream);
return reader.ReadToEnd();
}
Run Code Online (Sandbox Code Playgroud)
我尝试了以下顺序:
string GetResponse(string url, string data)
{
var client = new RestClient(url);
var request = new RestRequest("", RestSharp.Method.POST);
request.AddParameter("application/x-www-form-urlencoded", data);
var response = client.Execute(request);
return response.Content; …Run Code Online (Sandbox Code Playgroud) 我在本地工作一切,但是当我在运行Mono 2.10.9的Amazon EC2 Debian 6.0.6服务器上测试Twilio的服务时,我发现发送短信不再有效.我相信问题出在服务器配置的某个地方,因为我使用的是最新的Twilio(3.5.6),RestSharp(104.1)和JSON.NET(4.5.11)库,我可以保证下面使用的变量方法调用在运行时不为空.
var client = new Twilio.TwilioRestClient("{my account number}", "{my account token}");
var smsMessage = client.SendSmsMessage("{my account phone number}", "{my mobile phone number}", "Hello World!");
Run Code Online (Sandbox Code Playgroud)
使用ntop,我已经看到有一些字节被发送/接收到/形成这个请求,但smsMessage结果仍然是null.SendSMSMessage函数使用RestSharp库,特别是RestClient的Execute函数,它调用Deserialize方法并返回IRestResponse,在这种情况下Data字段为null.
我在网上看到一些迹象表明问题可能出在反序列化过程中,但是,由于我已经确定我使用的是最新版本的JSON.NET,我不知道在这里采取了哪些其他步骤.
您对解决问题有什么建议吗?您是否怀疑它是代码或服务器问题?你也有任何建议,使用Debian,我如何进一步调试这种交互,甚至可能从应用程序外部获得完整的字节响应?
当我在RestSharp中发出请求时:
var response = client.Execute<bool>(request);
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
"Unable to cast object of type 'System.Boolean' to type 'System.Collections.Generic.IDictionary`2[System.String,System.Object]'."
Run Code Online (Sandbox Code Playgroud)
这是完整的HTTP响应,每个Fiddler:
HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Mon, 01 Apr 2013 15:09:14 GMT
Content-Length: 5
false
Run Code Online (Sandbox Code Playgroud)
似乎一切都是响应的犹太人,那么是什么给出了?
此外,如果我通过返回一个简单的值而不是一个对象来对我的WebAPI控制器做一些愚蠢的事情并且这将解决我的问题,请随时提出建议.
我试图通过这些说明连接到Twitter API
https://dev.twitter.com/docs/auth/application-only-auth
这是我的代码:
var baseUrl = "http://api.twitter.com/";
var client = new RestClient(baseUrl);
var request = new RestRequest("/oauth2/token", Method.POST);
var concat = ConfigurationManager.AppSettings["TwitterConsumerKey"] + ":" +
ConfigurationManager.AppSettings["TwitterConsumerSecret"];
string encodeTo64 = concat.EncodeTo64();
request.AddHeader("Authorization", "Basic " + encodeTo64);
request.AddHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
request.AddBody("grant_type=client_credentials");
IRestResponse restResponse = client.Execute(request);
Run Code Online (Sandbox Code Playgroud)
EncodeTo64
static public string EncodeTo64(this string toEncode)
{
byte[] toEncodeAsBytes
= System.Text.ASCIIEncoding.ASCII.GetBytes(toEncode);
string returnValue
= System.Convert.ToBase64String(toEncodeAsBytes);
return returnValue;
}
Run Code Online (Sandbox Code Playgroud)
Response.Content如下
"{\"errors\":[{\"code\":170,\"label\":\"forbidden_missing_parameter\",\"message\":\"Missing required parameter: grant_type\"}]}"
Run Code Online (Sandbox Code Playgroud)
这部分错了吗?
request.AddBody("grant_type=client_credentials");
Run Code Online (Sandbox Code Playgroud)
我已经验证我的凭据是正确的(我之前收到了这个错误,但解决了它,所以应该没问题).
当我使用REST Sharp下面的代码时,我无法将其传递listOfSelectedTicketsIds为null.
请求Rest .net客户端
var _stDeveloperApi = new RestClient("http://127.0.0.1/");
var url = string.Format("api/v1/SignalR/MultiClickMode");
var listOfSelectedTicketsIds = new List<int> { 2, 3 };
var request = new RestRequest(url, Method.GET);
request.AddParameter("listOfSelectedTicketsIds", listOfSelectedTicketsIds, ParameterType.GetOrPost);
var response = _stDeveloperApi.Execute(request);
Run Code Online (Sandbox Code Playgroud)
Web API方法
[HttpGet]
public HttpResponseMessage MultiClickMode(List<int> listOfSelectedTicketsIds)
{
var response = new HttpResponseMessage();
try
{
}
catch (Exception ex)
{
response = Request.CreateResponse(HttpStatusCode.InternalServerError);
}
return response;
}
Run Code Online (Sandbox Code Playgroud) 我使用RestSharp,我想知道处理响应的最佳方法是什么。有ErrorMessage,ErrorException而且ResponseStatus在RestResponse但我怎么能检查请求是否成功?
我使用此代码。看起来还好吗?
if (response.ResponseStatus != ResponseStatus.Completed)
{
throw new ApplicationException(response.ErrorMessage);
}
Run Code Online (Sandbox Code Playgroud) 我试图从Harvest反序列化数据,但它失败了(没有错误):https://github.com/harvesthq/api#api-json
返回的数据如下所示:
更新 (请参阅底部的完整JSON响应)
我运行下面代码时的输出是一个带有x个帖子的列表,其中每个帖子包含一个id = 0
是否有一个设置或我错过的东西让它忽略/解析周围的[]?
[DeserializeAs(Name = "project")]
public class Project
{
public int id { get; set; }
//public string name { get; set; }
//[DeserializeAs(Name = "created-at")]
//public DateTime CreatedAt { get; set; }
}
// The following is the methods to request for testing
public List<Project> GetProjects()
{
var request = new RestRequest("projects", Method.GET);
request.RequestFormat = DataFormat.Json;
return Execute<List<Project>>(request);
}
private T Execute<T>(RestRequest request) where T : new()
{
var client …Run Code Online (Sandbox Code Playgroud) 我已经坚持了一段时间。我有一个JSON响应,向我发送包含句点的密钥。例如:“ cost_center.code”
如何将其放入对象?我没有收到任何错误,但该值只是作为null传入,并且未反序列化到我的班级中。
这是我的课程:
public class Result
{
public string company { get; set; }
public string first_name { get; set; }
public string email { get; set; }
public string employee_id { get; set; }
public string last_name { get; set; }
[DeserializeAs(Name="cost_center.code")]
public string cost_center { get; set; }
}
public class RootObject
{
public List<Result> result { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这是JSON响应:
{
"result": [
{
"company": "My Company",
"first_name": "First",
"email": "example@fakeaddress.com",
"employee_id": "123456789",
"last_name": "Last", …Run Code Online (Sandbox Code Playgroud) 我有一个RestRequest,我试图转换为HttpClient获取请求.有什么方法可以按照下面的方式发送参数吗?
private readonly IRestClient _restClient;
public Type GetInfo(string name)
{
var request = new RestRequest(url, Method.GET);
request.AddParameter("name", "ivar");
var response = _restClient.ExecuteRequest(request);
return ExecuteRequest<Type>(request);
}
Run Code Online (Sandbox Code Playgroud)