WebClient request fails because client is seen as outdated browser - httpwebrequest

I'm attempting to download an HTML response from a website but I keep getting the following response: The security settings for your Internet browser or device appear to be out of date. Please upgrade now.
I've attempted to use every User-Agent setting I could find but I'm still receiving the same error. I've also tried the HTTPWebRequest method and received the same error.
The request works fine when I open IE or Chrome and type URL.
Here is my code:
string url = #"https://www.royalmail.com/track-your-item?trackNumber=" + track.TrackingNumber;
WebClient client = new WebClient();
client.Headers.Add("user-agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)");
string content = client.DownloadString(url);
Example of URL that I'm trying to call: https://www.royalmail.com/track-your-item?trackNumber=FP065628015GB

You should use TLS1.2 as your security protocol for this site.
This line should fix your problem.
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

Related

VB.NET WebClient Proxy Not Connecting To Google

I'm working on a small application that can query google from the WebClient class in VB.NET. I noticed that the WebClient queries Google just fine without the proxy property being added, but I'd like to also have the capability of using proxies to connect to Google as well and it just isn't working. Here's the code that I have:
Try
Dim wc As New System.Net.WebClient()
'This line below will bring in the "User Agent" to make the HTTP request:
wc.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36")
Dim wp As New System.Net.WebProxy(TextBox1.Text)
wc.Proxy = wp
Dim HTML As String = wc.DownloadString("https://www.google.com/search?q=spider+man")
TextBox1.Text = HTML
Catch ex As Exception
MsgBox(ex.Message)
End Try
The problem is that NOTHING comes back even though I know that I'm using an ALIVE proxy (working public proxy) as it's connects through other browser applications perfectly to Google. Some how Google is blocking my application because it's apparently missing something. I have the headers setup correctly, so I know it can't be that (or at least I believe that I have them setup correctly).
Would appreciate any support.

Angular 2 and Symfony 3 (OAuth and API calls)

I have a project in Angular 2.4.0 where I want to call endpoints from a Symfony 3 REST API. Both projects are launched locally. To get rid of CORS errors in Http calls in Angular, I set some proxy rules as follows :
{
"/api/*": {
"target": "http://myapi.dev:8000",
"secure": false,
"changeOrigin": true,
"pathRewrite": {"^/api" : ""},
"logLevel": "debug"
}
}
The first step is the authentication with Google OAuth, so I open a new popup window (in my Angular project) :
window.open('api/connect/google', '_blank', 'location=yes,height=570,width=520,scrollbars=yes,status=yes');
Then I chose a Google account to authenticate with, then the API close the popup window automatically when successfully authenticated.
Then I call the API again to get the current logged-in user :
get(): Observable<User> {
return this.http.get('api/user')
.map((response: Response) => response.json())
.catch((error: any) => Observable.throw(error));
}
The problem is that the API throws the following :
Request URL:http://localhost:2222/api/user
Request Method:GET
Status Code:302 Found
Remote Address:127.0.0.1:2222
Access-Control-Allow-Origin:*
cache-control:no-cache, private
connection:close
content-type:json
date:Wed, 22 Mar 2017 10:20:32 GMT
location:http://myapi.dev:8000/login
server:nginx/1.11.10
transfer-encoding:chunked
x-debug-token:128b90
x-debug-token-link:http://myapi.dev:8000/_profiler/128b90
x-powered-by:PHP/7.1.3
Accept:application/json, text/plain, */*
Accept-Encoding:gzip, deflate, sdch, br
Accept-Language:fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4
Cache-Control:no-cache
Connection:keep-alive
Cookie:PHPSESSID=18c73caec383e91904dfd239d1a95faa
Host:localhost:2222
Pragma:no-cache
Referer:http://localhost:2222/
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36
It seems the API don't know I'm already authenticated and tries to redirect me to the /login route for every other Http calls I want to make.
The API works as following :
/login is a twig page with a link to /connect/google (the Google OAuth)
/connect/google allows you to choose one Google Account to authenticate with
If you call any API endpoint without being authenticated, it redirects you to /login
If you're authenticated, you can call every API endpoint
If I try all above Angular Http calls directly into the browser (eg: http://myapi.dev:8000/connect/google, http://myapi.dev:8000/user) everything works well.
I really have no idea where this issue comes from.
As you said in comments, Angular app is hosted on http://localhost:2222/, but api is hosted on http://myapi.dev:8000. Those two origins are completely different. It means that when myapi.dev will begin session, set cookie, it will be unavailable on localhost. Browser is not allowed to send cookies from different origins (due to CORS). That's why api doesn't see your session id key.
Possible ways to overcome problem:
Store both apps (angular and api) on the same origin (it means same domain, same protocol, and same port) - it's the easiest way.
Catch session ID cookie value (just after it will be set) and store it inside sessionStorage. Next, create Angular's request interceptor which will add SESSION cookie to all requests which're going to myapi.dev

Blackberry User-Agent and https Redirect url

I tried to access a HTTPS connection by entering the value in browser, it seems to work fine and redirects me to expected page/output. But when I tried the same using the code, I am unable to get the result. I tried of setting the UserAgent as (Mozilla/5.0 (BlackBerry; U; BlackBerry 9800; en-GB) AppleWebKit/534.1+ (KHTML, like Gecko) Version/6.0.0.141 Mobile Safari/534.1+). But no luck. I am getting trusted connection alert, when i click continue i get response code 302.How can i implement secure connection certificate to disable trusted connection alert.
I am used to httpsconnection to open url it returns the response code 302.Again i checked with
if (rc == HttpConnection.HTTP_TEMP_REDIRECT
|| rc == HttpConnection.HTTP_MOVED_TEMP
|| rc == HttpConnection.HTTP_MOVED_PERM) {
String location = conn.getHeaderField("location").trim();
System.out.println("location========"+location);
try {
Url = location;
newhttpConn = (HttpConnection) Connector.open(Url, Connector.READ_WRITE);
newhttpConn.setRequestMethod(HttpConnection.POST);
newhttpConn.setRequestProperty("User-Agent",
System.getProperty("browser.useragent"));
} catch (Exception e) {
System.out.println( e.toString());
}
But no use i am getting same 302 from redirect url.
EDITED:
Also please give some ideas to get the functions equivalent to HTTPClient to be work in HTTPSConnection. How can i get those functionalities. If there is no possibility to use Httpclient in blackberry then how can i utilize Blackberry https connection equivalent to Httpcleint (or) to get the automatic redirect using HTTPS Connection?
When connector.open(url) excutes i am getting like this
SSL:->CH
SSL:<-SH
SSL:<-SC
SSL:<-SHD
TLS:->CKE
SSL:->CCS
TLS:->F
TLS:<-F
in output console then wrong response displayed.
I am getting trusted connection alert, when i click continue i get response code 302.
This is a fully expected behaviour.
302 means the requested resource is on some other URI. In other words server instructs you to execute a redirect. You should investigate connection headers and in the one named "Location" you will find a new URI to continue with. Close/finalize you current connection and start a new one for the just got redirect URI.
UPDATE:
Usually good servers respond with HTTP 302 after a successful POST. This is a known Post/Redirect/Get pattern to prevent users from posting the same data twise on browser page refresh (user may press F5 in a desktop browser). So if the page you are accessing via POST is designed for usual browsers, then this may just mean your POST was successful and you don't need to execute a redirecting request. At least, if you need the response for some result evaluation then don't redirect using a POST, this time use a GET.

ASP.NET SMS Gateway API

I purchased a SMS gateway for which they gave me only HTTP and XML API but I need ASP.NET API to work on my project. Can anybody help me how to convert the below API to ASP.NET(c#) API.
HTTP API
http://indiansms.smsmaker.in/api/sendmsg.php?user=*********&pass=********&sender=Sender ID&phone=Mobile No&text=SMS&priority=Priority&stype=smstype
XML API
$data="<?xml version='1.0' encoding='utf-8'?>
<MESSAGE>
<USERNAME>username</USERNAME>
<PASSWORD>password</PASSWORD>
<TEXT>Hi, this is a test message</TEXT>
<PRIORITY>ndnd</PRIORITY>
<SENDER>SenderId</SENDER>
<MSGTYPE>normal</MSGTYPE>
<ADDRESS>*********</ADDRESS>
<ADDRESS>*********</ADDRESS>
</MESSAGE>";
How to convert this API to ASP.NET API.
I know that this question is unqualified to post it here. But I need it immediately.
I have done something similar from an .Net application where we used the http api to send sms. We used the webclient in .net to send messages.
using (var client = new WebClient())
{
client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR1.0.3705;)");
client.QueryString.Add("user", "username");
client.QueryString.Add("pass", "password");
// Add all the parameters you need
using (Stream stream = client.OpenRead("http://indiansms.smsmaker.in/api/sendmsg.php"))
{
// Do something with the response..
}
}

When an Adobe AIR app requests a resource from the server what shows as user agent?

When a browser like Chrome makes a request for a web page or image like a gravatar icon the server receives the userAgent information like so:
"Chrome/2.0 AppleWebKit/400.1.1 (KHTML, like Gecko)"
But if an Adobe AIR app makes a request for an image in the Image component what does the server receive? Is the same as a URLRequest?
"Chrome/2.0 AppleWebKit/400.1.1 (KHTML, like Gecko) AdobeAIR/3.0"
Background:
My AIR app shows Gravatar icon for the user and recently it stopped working. I'm wondering if it's gravatar is denying calls from user agents it doesn't recognize.
I've tried changing the userAgent to test the theory and it's throwing an error:
// 1195: Attempted access of inaccessible method userAgent through a reference with static type Class.
URLRequestDefaults.userAgent("chrome");
Update:
It looks like I need to set the user agent as a property and not a method. Was referring to this forum post. So I can change that might fix the gravatar issue but not answer how Adobe AIR apps appear to the server.
i hope this result's comes useful
Mozilla/5.0 (Windows; U; en-US) AppleWebKit/533.19.4 (KHTML, like
Gecko) AdobeAIR/3.1
also i cant realize that what's difference between requesting for an Image and URLRequest, they are same.