For some reason, I'm receiving a 403: Forbidden exception in Visual Studio when I try fetching JSON results from a web API I'm using. When I try using the web API URL in my web browser, everything shows up as it should. Any ideas?
The main code:
theURI = "http://isitup.org/duckduckgo.com.json";
HttpClient httpClient = new HttpClient();
httpClient.MaxResponseContentBufferSize = Int32.MaxValue;
var jsonResponse = await httpClient.GetStringAsync(theURI);
var rootObject = JsonConvert.DeserializeObject<StatusMessage.RootObject>(jsonResponse);
int statuscode = rootObject.status_code;
if (statuscode == 1)
{
txtStatus.Text = "Website is UP!";
}
else if (statuscode == 2)
{
txtStatus.Text = "Website is DOWN!";
}
else
{
txtStatus.Text = "Invalid domain!";
}
and the class file
class StatusMessage
{
public class RootObject
{
public string domain { get; set; }
public int port { get; set; }
public int status_code { get; set; }
public object response_ip { get; set; }
public object response_code { get; set; }
public object response_time { get; set; }
}
}
Seems like the server responds you 403 if you are using the default user agent. If you change it to chrome user agent the server is answering properly.
var url = new Uri("http://isitup.org/duckduckgo.com.json");
var req = new HttpClient();
var message = new HttpRequestMessage(HttpMethod.Get, url);
message.Headers.Add("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1309.0 Safari/537.17");
var response = await req.SendAsync(message);
var responseString = await response.Content.ReadAsStringAsync();
Related
I am calling an external service using GetAsync() and passing parameters in query string. When i check the content in the response, i don't see anything returned, however it returns 200 OK and in fiddler it returns me the XML response correctly. I need the XML response to get de-serialize to an C# object and then further save it to DB.
Things tried:
1) Tried this by adding this setting in global- app_start(), It didn't help
GlobalConfiguration.Configuration.Formatters.XmlFormatter.UseXmlSerializer = true;
2) Created an object and tried to sent it via GetAysnc, that didn't help either.
public class Request
{
[XmlElement]
public string XML { get; set; }
[XmlElement]
public List<string> ProNumber { get; set; }
}
2) Should i try passing parameters in query string and expect json result? if i add mediatyperformatter to application/json?
Here is my code:
public async Task<HttpResponseMessage> GetData()
{
string requestString = "&xml=Y&PRONumber=82040X,03117X";
string result = "";
string url = #"http://my.yrc.com/dynamic/national/servlet?CONTROLLER=com.rdwy.ec.rextracking.http.controller.PublicTrailerHistoryAPIController";
try
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(url);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml"));
HttpResponseMessage response = await client.GetAsync(url+requestString);
if (response.IsSuccessStatusCode)
{
return response;
}
}
}
catch (Exception ex)
{
result = ex.Message;
}
return null;
}
EDIT:
Shipments scp = null;
XmlRootAttribute xRoot = new XmlRootAttribute();
xRoot.ElementName = "Shipment";
xRoot.IsNullable = true;
XmlSerializer serializer = new XmlSerializer(typeof(Shipment), xRoot);
using (Stream stream = response.Content.ReadAsStreamAsync().Result)
{
scp = (Shipments)serializer.Deserialize(stream);
}
Model:
public class Shipments
{
[XmlArrayItem(Type = typeof(Shipment))]
public Shipment[] Shipment;
}
public class Shipment
{
[XmlAttribute()]
public int returnCode { get; set; }
.................
..............
Getting error:<SHIPMENTS xmlns=''> was not expected.
Any help on this is much appreciated.
Thanks,
WH
This worked for me -
var client = new HttpClient();
var data = client.GetStringAsync("http://my.yrc.com/dynamic/national/servlet?CONTROLLER=com.rdwy.ec.rextracking.http.controller.PublicTrailerHistoryAPIController&xml=Y&PRONumber=82040X,03117X").Result;
var ser = new XmlSerializer(typeof(Shipments));
var t = (Shipments)ser.Deserialize(new StringReader(data));
public class Shipment
{
public string returnCode { get; set; }
public string returnMessage { get; set; }
public string freightBillNumber { get; set; }
//props
}
[XmlRoot(ElementName = "SHIPMENTS")]
public class Shipments
{
[XmlElement(ElementName = "SHIPMENT")]
public List<Shipment> SHIPMENT { get; set; }
}
EDIT
this works as well -
var data = client.GetStreamAsync("http://my.yrc.com/dynamic/national/servlet?CONTROLLER=com.rdwy.ec.rextracking.http.controller.PublicTrailerHistoryAPIController&xml=Y&PRONumber=82040X,03117X").Result;
EDIT
works as well -
var client = new HttpClient();
var data = client.GetAsync("http://my.yrc.com/dynamic/national/servlet?CONTROLLER=com.rdwy.ec.rextracking.http.controller.PublicTrailerHistoryAPIController&xml=Y&PRONumber=82040X,03117X").Result;
var ser = new XmlSerializer(typeof(Shipments));
var t = (Shipments)ser.Deserialize(data.Content.ReadAsStreamAsync().Result);
I have this code:
private const string route = "/api/Print";
public bool Update(string header, string tc)
{
bool success = false;
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("my uri");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var print = new Print { CompanyRef = new Guid(), Header = header, TC = tc };
var response = client.PutAsJsonAsync(route, print);
}
success = true;
return success;
}
public sealed class Print
{
public string Header { get; set; }
public string TC { get; set; }
public System.Guid CompanyRef { get; set; }
}
I call it like so:
Update(" header", " string tc");
In C# desktop app it works.
In Windows 10 IoT on a Raspberry Pi2 device it does not work.
Yet, when i am calling a Get from my Web API server *in Iot) it works fine.
?
I am using this code for a year now and it works:
using Windows.Web.Http;
using (HttpClient httpClient = new HttpClient())
{
httpClient.DefaultRequestHeaders.Add("Cache-Control", "no-cache");
try
{
var o = new
{
operation = "NewEvent",
location_id = locationID,
eventName = eventName
};
HttpStringContent content = new HttpStringContent(JsonConvert.SerializeObject(o), Windows.Storage.Streams.UnicodeEncoding.Utf8, "application/json");
HttpResponseMessage response = await httpClient.PostAsync(new Uri(urlPostData), content);
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
// TODO: Do something with the responseBody
}
catch (Exception)
{
// TODO: Deal with exception - could be a server not found, 401, 404, etc.
}
}
I am just beginning developing using RestSharp and have hit an early roadblock. I think once I understand this simple, but key, concept, I should be off and running. I need to return an Access Token before making my standard calls later. I have set up the following classes, generated from json2csharp.com:
public class AccessToken
{
public string Instance_Url { get; set; }
public string Token { get; set; }
public string Expiration_date { get; set; }
public string Refresh_Token { get; set; }
}
public class RootObject
{
public AccessToken Access_Token { get; set; }
}
I have coded the following on a button click:
var tokenclient = new RestClient();
tokenclient.BaseUrl = "https://url";
tokenclient.Authenticator = new HttpBasicAuthenticator("username", "password");
var tokenrequest = new RestRequest(Method.GET);
tokenrequest.RequestFormat = DataFormat.Json;
IRestResponse tokenresponse = tokenclient.Execute(tokenrequest);
var content = tokenresponse.Content;
RestSharp.Deserializers.JsonDeserializer deserial = new JsonDeserializer();
var des = deserial.Deserialize<AccessToken>(tokenresponse);
I am able to return the following JSON as a string:
{
"Access_Token": {
"Instance_Url": "https://url",
"Token": "StringToken",
"Expiration_date": "9/30/2015 6:15:27 PM",
"Refresh_Token": "StringToken"
}
}
However, when I pull des.Token, it returns a blank value. Can somebody kindly point out my error?
using Newtonsoft.Json;
var response = client.DownloadString(url + queryString);
ResponseModel<string> dataResponse = new ResponseModel<string>();
if (!string.IsNullOrEmpty(response))
{
dataResponse = JsonConvert.DeserializeObject<ResponseModel<string>>(response);
}
I am using WPF as Client and Web api as Service. For add new event click button and update button in WPF not returning to http post action in api controller and also returning error Internal server error.
Http Post method
[HttpPost]
public HttpResponseMessage PostEvent(EventFormModel event1)
{
if (ModelState.IsValid)
{
var command = new CreateOrUpdateEventCommand(event1.EventId, event1.EventAgenda, event1.Description, event1.EventDate, event1.Location, event1.UserId);
var result = commandBus.Submit(command);
if (result.Success)
{
var response = Request.CreateResponse(HttpStatusCode.Created, event1);
string uri = Url.Link("DefaultApi", new { id = event1.EventId });
response.Headers.Location = new Uri(uri);
return response;
}
}
else
{
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
}
throw new HttpResponseException(HttpStatusCode.BadRequest);
}
WPF click event:
private async void btnNewEvent_Click(object sender, RoutedEventArgs e)
{
var event1 = new Event()
{
EventAgenda = txtAgenda.Text,
Description = txtEventDescription.Text,
Location=txtLocation.Text,
UserId=Convert.ToInt32(txtUserId.Text),
EventId=Convert.ToInt32(txtEventId.Text),
EventDate=Convert.ToDateTime(dateEventDate.Text),
};
client.BaseAddress = new Uri("http://localhost:40926/api/Event");
// Add an Accept header for JSON format.
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
var response = client.PostAsJsonAsync("api/Event", event1).Result;
if (response.IsSuccessStatusCode)
{
MessageBox.Show("added" + response);
txtAgenda.Text = "";
txtEventDescription.Text = "";
txtLocation.Text = "";
txtUserId.Text = "";
txtEventId.Text = "";
dateEventDate.Text = "";
}
else
{
MessageBox.Show("Error Code" + response.StatusCode + " : Message - " + response.ReasonPhrase);
}
}
Event class in WPF application:
public class Event
{
public int EventId { get; set; }
public int UserId { get; set; }
// [Required(ErrorMessage = "Agenda Required")]
//[Display(Name = "Movie Name")]
public string EventAgenda { get; set; }
// [Required(ErrorMessage = "Location Required")]
public string Location { get; set; }
// [Required(ErrorMessage = "Date Required")]
public DateTime EventDate { get; set; }
public string Description { get; set; }
// public string ReminderType { get; set; }
}
I used breakpoints near post action and also click event. but in click event near
var response = client.PostAsJsonAsync("api/Event", event1).Result; not returing to api post method and returning Response status code does not indicate success: 500 (Internal Server Error). Similar issue for Update also
-Thanks Sindhu
You included some parts of the URI twice.
client.BaseAddress = new Uri("http://localhost:40926/api/Event");
// Add an Accept header for JSON format.
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
var response = client.PostAsJsonAsync("api/Event", event1).Result;
You duplicated "api/Event". And would get the URI, api/Event/api/Event/. For your BaseAddress you only need hostname and port.
e.g.
client.BaseAddress = new Uri("http://localhost:40926");
var response = client.PostAsJsonAsync("api/Event", event1).Result;
Also you are getting a 500 Internal Server error from somewhere else in your sever code. I can't where right now.
I am having trouble sending a list of objects to a webapi controller.
This is the controler:
[AcceptVerbs("POST")]
public string syncFoodData(List<intakeSync> str)
{
string apikey = Request.Headers.GetValues("api_key").ToArray()[0];
return "data syncronised";
}
This is the class:
public class intakeSync
{
public int memberID { get; set; }
public Double size { get; set; }
public string food { get; set; }
public string token { get; set; }
public string time { get; set; }
public string date { get; set; }
public string nocatch { get; set; }
public string calories { get; set; }
}
The value of str is always null.
this is the webmethod that sends the httprequest to the webapi
public static string syncIntakeData(string token, string[] syncString)
{
JavaScriptSerializer js = new JavaScriptSerializer();
List<intakeSync> str = new List<intakeSync>();
for (int i = 0; i <= syncString.Length - 1; i++)
{
str.Add(js.Deserialize<intakeSync>(syncString[i]));
}
string url = URI + "/api/Food/?str=" +js.Serialize(str);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.Headers.Add("api_key", token);
Stream requestStream = request.GetRequestStream();
StreamReader read = new StreamReader(request.GetResponse().GetResponseStream());
string dat = read.ReadToEnd();
read.Close();
request.GetResponse().Close();
return dat;
}
You can use this :
Request body in Json
[{id:1, nombre:"kres"},
{id:2, nombre:"cruz"}]
Api Rest .net C#
public string myFunction(IEnumerable<EntitySomething> myObj)
{
//...
return "response";
}
I don't know really how your JSON is serialized in the line js.Serialize(str); I suspect that this line is the core problem. Sending JSON is better suited in the POST Request body than in the query string. Anyways, I think that HttpClient is better suited for working with WebApi because it offers symmetric programming experience. You could try something like that with HttpClient :
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(URI);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("api_key", token);
var content = new ObjectContent(syncString, new JsonMediaTypeFormatter());
var result = client.PostAsync("/api/Food/", content).Result;
}