I need to connect to a website using a proxy server. I can do this manually, for example I can use the online proxy http://zend2.com and then surf to www.google.com. But this must be done programmatically. I know I can use WebProxy class but how can I write a code so a proxy server can be used?
Anyone can give me a code snippet as example or something?
thanks
Understanding of zend2 works, you can populate an url like this :
http://zend2.com/bro.php?u=http%3A%2F%2Fwww.google.com&b=12&f=norefer
for browsing google.
I C#, build the url like this :
string targetUrl = "http://www.google.com";
string proxyUrlFormat = "http://zend2.com/bro.php?u={0}&b=12&f=norefer";
string actualUrl = string.Format(proxyUrlFormat, HttpUtility.UrlEncode(targetUrl));
// Do something with the proxy-ed url
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(new Uri(actualUrl));
HttpWebResponse resp = req.GetResponse();
string content = null;
using(StreamReader sr = new StreamReader(resp.GetResponseStream()))
{
content = sr.ReadToEnd();
}
Console.WriteLine(content);
You can use WebProxy Class
MSDN code
WebProxy proxyObject = new WebProxy("http://proxyserver:80/",true);
WebRequest req = WebRequest.Create("http://www.contoso.com");
req.Proxy = proxyObject;
In your case
WebProxy proxyObject = new WebProxy("http://zend2.com",true);
WebRequest req = WebRequest.Create("www.google.com");
req.Proxy = proxyObject;
Related
I upgraded my .NET application from the version NET5 to NET6 and placed with a warning that the WebRequest class was obsolete. I've looked at a few examples online, but using HttpClient doesn't automatically grab credentials like WebRequest does, as you need to insert them into a string manually.
How would I convert this using the HttpClient class or similar?
string url = "website.com";
WebRequest wr = WebRequest.Create(url);
wr.Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse hwr = (HttpWebResponse)wr.GetResponse();
StreamReader sr = new(hwr.GetResponseStream());
sr.Close();
hwr.Close();
Have you tried the following?
var myClient = new HttpClient(new HttpClientHandler() { UseDefaultCredentials = true });
var response = await myClient.GetAsync("website.com");
var streamResponse = await response.Content.ReadAsStreamAsync();
more info around credentials: How to get HttpClient to pass credentials along with the request?
I am developing one application where I need capture basic detail like title, description and images of website based on url provided by user.
But user may be enter www.google.com insted of http://www.google.com but C#.net code failed to retrieve data for "www.google.com" through below code
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri(url));
request.Method = WebRequestMethods.Http.Get;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
String responseString = reader.ReadToEnd();
response.Close();
and found error like "Invalid URI: The format of the URI could not be determined."
So do know any technique to found full url based on shorten url.
for ex. google.com or www.google.com
Expected output : http://www.google.com or https://www.google.com
PS : I found online web tool (http://urlex.org/) that will return full url based on shorten url
Thanks in advance.
You can use UriBuilder to create a URL with HTTP as default scheme:
UriBuilder urb = new UriBuilder("www.google.com");
Uri uri = urb.Uri;
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
request.Method = WebRequestMethods.Http.Get;
string responseString;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
responseString = reader.ReadToEnd();
}
}
If your URL contains a scheme, it will use that one instead of the default HTTP scheme. I have also used using to release all unmanaged resources.
So do know any technique to found full url based on shorten url.
I may have misunderstood your issue here but can't you just append "http://" if it's missing?
string url = "www.google.com";
if (!url.StartsWith("http"))
url = $"http://{url}";
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri(url));
request.Method = WebRequestMethods.Http.Get;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
String responseString = reader.ReadToEnd();
}
This is basically what a web browser does when you don't specify any protocol.
In the past I have successfully called a JSON webservice over HTTP
But, now I have to make a JSON POST over HTTPS.
I have tried using the code that works for HTTP and simply changed the url that is being called to https but it won't work.
This is the code i am using...
WebRequest wrGETURL;
wrGETURL = WebRequest.Create("https://apitest.example.com/geo/coverage/v1/?appId=2644571&appKey=836621d715b6ce4db5f007d8fa2214f");
wrGETURL.Method = "POST";
Stream objStream;
objStream = wrGETURL.GetResponse().GetResponseStream();
StreamReader objReader = new StreamReader(objStream);
string responseFromServer = objReader.ReadToEnd();
and the error message i am seeing in fiddler is:
fiddler.network.https> Failed to secure existing connection for apitest.example.com. A call to SSPI failed, see inner exception. InnerException: System.ComponentModel.Win32Exception (0x80004005): The client and server cannot communicate, because they do not possess a common algorithm
Can anyone help me with that I need to do to make a call over HTTPS please?
Do you need to authenticate or maybe a callback for the server certificate?
This works for me in most cases:
var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://someurl/");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
// Create NetworkCredential Object
NetworkCredential admin_auth = new NetworkCredential("username", "password");
// Set your HTTP credentials in your request header
httpWebRequest.Credentials = admin_auth;
// callback for handling server certificates
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = "{\"name\":\"TEST_123\"}";
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
Console.WriteLine(result);
}
}
I am using React and Expressjs and ran into this issue. Send your ie: https://www.google.com with out the https:
part of my router.js
router('/vod')
.rout.get('/vod', (req,res)=>{
res.send({message: "//www.google.com"});
App.js
function App() {
const [vod, setVod] = React.useState(null);
React.useEffect(() => {
fetch("/vod")
.then((res) => res.json())
.then((vod) => setVod(vod.message));
},
[]);
return (
<div className="App">
<header className="App-header">
<iframe src={"https:"+vod}
id="myIframe" autoPlay width={1000} height=
{500} frame></iframe>
</header>
</div>
);
in the iframe to change the source I used {"https:"+vod} to simulate the full url.
in your case try to combine your "result" like ("https:"+result)
I noticed that json grabs the : and messes the string up.
I am using GitHub API v3 with C#. I am able to get the access token and using that I am able to get the user information and repo info.
But when I try to create a new repo I am getting the error as Unauthorized.
I am using HttpWebRequest to post data, which can be seen below. Please suggest me some C# sample or sample code.
(..)string[] paramName, string[] paramVal, string json, string accessToken)
{
HttpWebRequest req = WebRequest.Create(new Uri(url)) as HttpWebRequest;
req.Method = "POST";
req.ContentType = "application/json";
StreamWriter writer = new StreamWriter(req.GetRequestStream());
writer.Write(json);
writer.Close();
string result = null;
using (HttpWebResponse resp = req.GetResponse() as HttpWebResponse)
{
StreamReader reader =
new StreamReader(resp.GetResponseStream());
result = reader.ReadToEnd();
}(..)
Note: I am not sure where i need to add the accesstoken. I have tried in headers as well as in the url, but none of them works.
Are you using the C# Github API example code? I would look at that code to see if it does what you need.
You can use basic auth pretty easily by just adding auth to the request headers:
request.Headers.Add("Authorization","Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(username +":"+password)));
Sorry havent used the access token stuff yet.
You need to add this token here:
req.UserAgent = "My App";
req.Headers.Add("Authorization", string.Format("Token {0}", "..token..");
Try
rec.Credentials = CredentialCache.DefaultCredentials;
or use non-default credentials.
http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.credentials.aspx
I'm trying to open adfoc.us/504....9 link with httpwebrequest.
However it gives me no HTML code.
try
{
req = WebRequest.Create(txtLink.Text);
WebProxy wp = new WebProxy(proxies[0]);
//req.Proxy = wp;
WebResponse wr = req.GetResponse();
StreamReader sr = new StreamReader(wr.GetResponseStream());
string content = sr.ReadToEnd();
MessageBox.Show(content);
sr.Close();
}
catch (UriFormatException)
{
MessageBox.Show("URL should be in this format:\nhttp://www.google.com");
return;
}
If I use website like [google.com][1] - I get mbox with google html source.
If I use adfoc.us/50.... link I get an empty string.
Where could be the problem?
Thank you.
EDIT: I resolved the problem by installing GeckoFx component.
This is just a guess.
If you can open the link in your browser and not from your code it could mean that adfoc.us blocks you because it can't find the useragent header. Try adding a useragent header that a browser uses.
try this
var req = (System.Net.HttpWebRequest) System.Net.WebRequest.Create("");
req.AllowAutoRedirect = true;
and you can manual set MaximumAutomaticRedirections
When initializing the WebRequest, add the following:
req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
Seems like it doesn't like the default header. I got the above from Firefox request header.