Response status : 404 using HttpClient in wp8.0 - windows-phone-8

I had this code working but now suddenly it does not return
"Response status code does not indicate success:404()"..
what ever may be the URL it doesn't matter..
below is the code:
protected async void getstring(string url)
{
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0");
try
{
var html = await client.GetStringAsync(url);
extractData(html);
}
catch (HttpRequestException e)
{
MessageBox.Show(e.Message);
arrivalTime.Text = e.Message;
}
//time.Text = html;
}

Related

Google App Script: GAS return 403 but browser requests still work

I'm using google app script to monitoring URL is working then writing log in excel after every 30 mins:
try {
var options = {
"method" : "GET",
'headers': {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.93 Safari/537.36'
},
'muteHttpExceptions': true
};
var url_trimmed = url.trim();
var response = UrlFetchApp.fetch(url_trimmed, options);
Logger.log(response.getContentText());
var result = response.getResponseCode();
return result;
}
catch (err) {
return [EXCEPT_ERR_CD, Utilities.formatString('statusCode: %s、Error: %s)', EXCEPT_ERR_CD, err)];
}
}
But when I check Excel result file, result sometimes return responseCode 200, and sometimes return 403. Immediately when responseCode returns 403, I check it in browser but site is working fine.
I tried add header parameter but it doesn't work.
I dont have any idea, I think maybe the error from the site but dont really know causes and how to fix it.

signin-oidc flow throws 500 in chrome but not IE

I have a netcoreapp2.1 webapp which is presenting an issue in Google Chrome, but not Edge or IE, during the OIDC flow redirect. It simply stops at http://localhost:5000/signin-oidc with this response received:
General:
Request URL: http://localhost:5000/signin-oidc
Request Method: POST
Status Code: 500 Internal Server Error
Remote Address: [::1]:5000
Referrer Policy: no-referrer-when-downgrade
Response Headers:
Content-Length: 0
Date: Tue, 04 Aug 2020 09:49:32 GMT
Server: Kestrel
Request Headers:
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Accept-Encoding: gzip, deflate, br
Accept-Language: en-GB,en;q=0.9
Cache-Control: max-age=0
Connection: keep-alive
Content-Length: 2148
Content-Type: application/x-www-form-urlencoded
Host: localhost:5000
Origin: null
Sec-Fetch-Dest: document
Sec-Fetch-Mode: navigate
Sec-Fetch-Site: cross-site
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36
Form Data:
code: Ry9Pec...rdG1U0SB
scopes: resource.WRITE+openid+resource.READ
state: CfDJ8JZ...oESbUvQ
id_token: eyJhbG...9sXMEhs
When I visit localhost:5000 in chrome (incognito or not) the code never reaches the OnTicketReceived event, whereas when fired from IE, Edge etc it does, and proceeds just fine.
This is the startup class:
public class Startup
{
private AppSettings _appSettings;
private IConfiguration _config;
public Startup(IConfiguration configuration)
{
_config = configuration;
_appSettings = _config.Get<AppSettings>();
}
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.Configure<AppSettings>(_config);
services.AddSingleton<IAPIRepository, APIRepository>();
services.AddSingleton<IUserRepository, UserRepository>();
services.AddSingleton<INavigationRepository, NavigationRepository>();
services.UseOpenIDConnectMiddleware(new OpenIDConnectMiddlewareOptions
{
BaseUrl = _appSettings.API.BaseUrl,
AppName = _appSettings.AppName,
ClientId = _appSettings.API.ClientId,
ClientSecret = _appSettings.API.ClientSecret,
Secure = !_appSettings.Local
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseAuthentication();
if (!_appSettings.Local)
{
app.UseGlobalLoginMiddleware();
}
if (_appSettings.FeatureFlags["ProcessRedirectionRules"])
{
app.UseRedirectUserMiddleware(_appSettings.RedirectionRules);
}
app.UseMvc();
app.UseProtectHTMLRouteMiddleware();
if (_appSettings.Local)
{
app.UseDeveloperExceptionPage();
app.UseWebpackDevMiddleware(new WebpackDevMiddlewareOptions {
ProjectPath = Path.Join(Directory.GetCurrentDirectory(), "../UI"),
ConfigFile = "node_modules/#vue/cli-service/webpack.config.js",
HotModuleReplacement = true
});
}
else
{
app.UseDefaultFiles();
app.UseStaticFiles();
}
app.UseJavascriptVarMiddleware(new JavascriptSettingsMiddlewareOptions
{
FileName = "env.js",
ObjectName = "__env",
Settings = new Dictionary<string, string>
{
{ "insightsKey", _appSettings.ApplicationInsights.InstrumentationKey },
{ "environment", _appSettings.Environment },
{ "gatekeeperBaseUrl", _appSettings.Gatekeeper.BaseUrl }
}
});
app.UseGlobalSignoutMiddleware(new GlobalSignoutMiddlewareOptions
{
GatekeeperLogoutUrl = _appSettings.API.LogoutUrl
});
app.UseHTMLRouteFallback(new HTMLRouteFallbackMiddlewareOptions
{
Local = _appSettings.Local,
Path = Path.Join(Directory.GetCurrentDirectory(), "../UI/dist")
});
}
}
I have cleaned the localhost and localhost:5000 cookies from the browser.
A quick guess would be that it is a SameSite cookie issue that different browsers handle differently due to various bugs.
This article might give you a starting point:
How To Prepare Your IdentityServer For Chrome's SameSite Cookie Changes - And How To Deal With Safari, Nevertheless
Also, your Configure method looks very odd. Because the ordering of the App.Use statements matter and usually you would put the app.UseMvc(); last in that method. Each incoming request will pass through those middlewares that the App.UseXXX adds and if the ordering is not correct then you will have issues.

Upload product image with product name from xamarin form to web api

i am using entity framework in web API for posting product details of name, image and price from Xamarin form to web API to save product details in API. But i don't know how to save image on web API folder. please anyone guide me how to do that....
Umar sir
below code i am using but i don't know how to save image in web server folder and database.
--------------------
web API call from Xamarin form
---------------------------
var request = new HttpRequestMessage();
string url = "https://example.com/api/Product;
request.Method = HttpMethod.Post;
request.Headers.Add("Accept", "application/json");
var client = new HttpClient();
client.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36");
//file to base64 string
byte[] b = System.IO.File.ReadAllBytes(txtImage.Text);
String s = Convert.ToBase64String(b);
tblProduct product= new tblProduct()
{
ProductName = txtCategoryName.Text,
Price = txtPrice.Text,
ProductImage = s
};
var content = new StringContent(JsonConvert.SerializeObject(product), Encoding.UTF8, "application/json");
HttpResponseMessage response = client.PostAsync(url, content).Result;
--------------------
web API code
--------------------
public HttpResponseMessage Post(tblProduct product)
{
HttpResponseMessage response = null;
using (orderapp_dbEntities entity = new orderapp_dbEntities())
{
var img= Convert.FromBase64String(product.ProductImage);
tblProduct prd = new tblProduct()
{
ProductName = product.ProductName,
Price=product.Price,
ProductImage = img
};
entity.tblProducts.Add(prd);
entity.SaveChanges();
response = Request.CreateResponse(HttpStatusCode.Created, prd);
return response;
}
}

UrlFetchApp.fetch() doesn't seem to change user agent

Trying to grab data from a website using Google Apps Script to put it directly into a spreadsheet. The fetch does not seem to be working, where the Python requests equivalent works just fine.
Python code:
page = requests.get("someurl?as_data_structure", headers={'user-agent':'testagent'})
GAS code:
var page = UrlFetchApp.fetch("someurl?as_data_structure", headers={'user-agent':'testagent'});
The only required header is the user-agent, and the error I am getting from the GAS code is what I would usually get from the Python code if I hadn't included the header. I am new to js but as far as I know this is the proper way to do it..?
EDIT:
Now got the headers in the right place but the issue persists, exactly the same error as before.
var options = {"headers": {"User-Agent": "testagent"}};
var page = UrlFetchApp.fetch("someurl?as_data_structure", options);
Star ★(on top left) the issue here for Google developers to prioritize the issue.
Google doesn't always document it's restrictions(Annoying?). One such restriction is changing the user agent. It's fixed to
"User-Agent": "Mozilla/5.0 (compatible; Google-Apps-Script)"
You can't change it.
Sample Test:
function testUrlFetchAppHeaders() {
var options = {
headers: {
'User-Agent':
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36',
},
};
var fakeRequest = UrlFetchApp.getRequest(
'https://www.httpbin.org/headers',
options
);//providing fake assurance
var realRequest = UrlFetchApp.fetch(
'https://www.httpbin.org/headers',
options
);//like a wrecking ball
Logger.log({ fake: fakeRequest, real: realRequest });
}
Sample Response:
{
"fake": {
"headers": {
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"
},
"method": "get",
"payload": "",
"followRedirects": true,
"validateHttpsCertificates": true,
"useIntranet": false,
"contentType": null,
"url": "https://www.httpbin.org/headers"
},
"real": {
"headers": {
"Accept-Encoding": "gzip,deflate,br",
"Host": "www.httpbin.org",
"User-Agent": "Mozilla/5.0 (compatible; Google-Apps-Script)"
}
}
}
getRequest(url)
Returns the request that would be made if the operation was invoked.
This method does not actually issue the request.
Neither does it accurately return the request that would be made.
the headers belong into the options:
var options = {"headers": {"User-Agent": "testagent"}};
var page = UrlFetchApp.fetch("someurl?as_data_structure", options);

HttpPost generating results but not sending back to angular controller

I’ve been battling this problem now for a few hours, and have tried several solutions (including using dataset, datareader to access the data, converting the datareader/dataset into a list of objects and then sending the result as a Json. I’ve also tried using Json.Net and a replacing the $http with an AJAX call, all to no avail.
In an MVC4 project, I’ve detailed an angular controller to retrieve data from our ACCESS database. An $http request is sent to the (local)server, received, and executed. The results generated, however, are not received back by the angular controller.
Any and all help will be greatly appreciated
This is the method:
[HttpPost]
public JsonResult GetCatalogueItems(string get)
{
JsonResult toReturn = null;
string cs = ConfigurationManager.ConnectionStrings["connStringPT"].ConnectionString;
OleDbConnection cn = new OleDbConnection(cs);
string queryStart = " SELECT catNum, testName, price, comments FROM table_catalog WHERE ";
DataSet ds = new DataSet();
try
{
List<ptProductModel> listToReturn = new List<ptProductModel>();
string query = queryStart + get + "=true;";
cn.Open();
OleDbCommand cmd = new OleDbCommand(query, cn);
OleDbDataReader reader = cmd.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
ptProductModel item = new ptProductModel
{
Number = reader[0].ToString(),
Name = reader[1].ToString(),
//comment = reader.GetString(reader.GetOrdinal("comments")) !=null ? reader.GetString(reader.GetOrdinal("comments")) : null,
price = reader[2].ToString()
};
listToReturn.Add(item);
}
reader.Close();
if (cn.State == ConnectionState.Open)
{
cn.Close();
}
toReturn = Json(listToReturn.Select(s => new { Name = s.Name, price = s.price, Number = s.Number }), JsonRequestBehavior.AllowGet);
}
}
catch (OleDbException ex)
{
toReturn = Json("Error", JsonRequestBehavior.AllowGet);
}
return toReturn;
}
}
And this is the angular controller:
myApp.controller('ptCatalogueController', ['$scope', '$http', function ($scope, $http) {
$scope.catalogueItems = [];
$scope.Product = "";
$scope.Init = function () { }
$scope.GetProducts = function (productSet) {
console.error(productSet, 'blabla');
$http.post({
url: '/Catalogue/GetCatalogueItems', data: { get: productSet }
}).then(
// success callback
function (data) {
$scope.catalogueItems = data;
console.log($scope.catalogueItems);
console.debug("success");
}
,
//error callback
function (response) {
console.log(response);
console.debug("error");
}
)}
}]);
and the header:
POST /Catalogue/[object%20Object] HTTP/1.1
Host: localhost:49078
Accept: application/json, text/plain, */*
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8,he;q=0.6
Cookie: ASP.NET_SessionId=fvfswezab5g1ekyiuotmpw3m
Origin: http://localhost:49078
Referer: http://localhost:49078/Catalogue/Index
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36
X-DevTools-Emulate-Network-Conditions-Client-Id: 0689EBCB-E24D-48E3-ACF9-0D94BB0B6F16
HTTP/1.1 404 Not Found
Cache-Control: private
Content-Length: 3732
Content-Type: text/html; charset=utf-8
Date: Sun, 22 Nov 2015 07:59:03 GMT
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
X-SourceFiles: =?UTF-8?B? RTpcR2VuZXJhbCBGb2xkZXJcRGV2ZWxvcG1lbnRcV2ViLVBzeWNoVGVjaC1OZXdTaXRlXFdlYi1Qc3ljaFRlY2gtTmV3U2l0ZVxDYXRhbG9ndWVcW29iamVjdCBPYmplY3Rd?=