Script Examples for Establishing External HTTP Connections (Windows)
Please use the “Print” function at the bottom of the page to create a PDF.
For IONOS Windows Web Hosting Packages
If a program or script is designed to read data from another resources on the Internet, such as a website or another file, the connection has to be made using the HTTP proxy server of IONOS (winproxy.server.lan). If this is not done, the connection will be rejected for security reasons.
There are two ways to use the HTTP proxy for external connections: Configuring the web.config file or writing it into a program.
Option 1: Enable HTTP Proxy in web.config
Since the proxy must always be used automatically for HTTP requests, you must configure the web.config file. For doing this, the host winproxy.server.lan has to be registered as defaultProxy in the <system.net> ... </system.net> section:
<configuration>
...
<system.net>
<defaultProxy>
<proxy proxyaddress="http://winproxy.server.lan:3128"
bypassonlocal="true"
/>
</defaultProxy>
</system.net>
...
</configuration>
Option 2: Enable HTTP Proxy in a Script
As an alternative to web.config configuration, the proxy can also be temporarily activated for requests.
The following code examples for VB.NET and C# show how to activate the HTTP proxy using the WebRequest.Proxy property of the WebRequest class (from the system.net namespace):
VB.NET
<% @Import Namespace="System.Net" %>
<% @Import Namespace="System.IO" %>
<script language="VB" runat=server>
Sub Page_Load(sender as object, e as System.EventArgs)
Try
Dim wrq As WebRequest = WebRequest.Create("http://domain.tld") //replace domain.tld as wanted
wrq.Proxy = new WebProxy("http://winproxy.server.lan:3128")
Dim wrp As WebResponse = wrq.GetResponse()
Dim sr as StreamReader = new StreamReader(wrp.GetResponseStream(), Encoding.ASCII)
While sr.Peek() > 0
Response.Write(sr.ReadLine())
End While
Catch ex as WebException
Response.Write(ex.Message)
End Try
End Sub
</script>
C#
<% @Import Namespace="System.Net" %>
<% @Import Namespace="System.IO" %>
<script language="C#" runat=server>
void Page_Load(object sender, System.EventArgs e)
{
try
{
WebRequest wrq = WebRequest.Create("http://domain.tld"); // replace domain.tld as wanted
wrq.Proxy = new WebProxy("http://winproxy.server.lan:3128");
WebResponse wrp = wrq.GetResponse();
StreamReader sr = new StreamReader(wrp.GetResponseStream(), Encoding.ASCII);
while (sr.Peek() > 0)
{
Response.Write(sr.ReadLine());
}
}
catch(WebException ex)
{
Response.Write(ex.Message);
}
}
</script>
Application Example (ASP.NET)
The following are code examples of a simple ASP.NET Web application that you can use to verify the functionality of the HTTP proxy. This is done by sending an incomplete request to the Google reCAPTCHA API, which is commonly used to verify a user's response to a recAPTCHA challenge. The response from the Google server will then be displayed on the website.
Please note that this is not a complete implementation of Google reCAPTCHA. Only the request necessary for a recAPTCHA Challenge is simulated without a corresponding reCAPCHTA.
- Create two files named default.aspx and default.aspx.cs on your webspace and copy the code below into them.
- Open the default.aspx page from your domain in the browser.
- Click Start to send a request and display the response immediately before the page's input field.
- Check whether a text is displayed to the left of the input field. The output should look like this:
{"success": false, "error-codes": [ "missing-input-response", "missing-input-secret" ] }
It is only important that there is something there and that there is no system error. If you cannot correct the latter, check whether the proxy can be activated alternatively using the web.config method mentioned earlier.
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" Codefile="default.aspx.cs" Inherits="WebApplication1._default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="URL:"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server" Width="269px" Text="https://www.google.com/recaptcha/api/siteverify"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Start" OnClick="Button1_Click" />
</div>
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
</form>
</body>
</html>
Default.aspx.cs
using System;
using System.Net;
using System.IO;
namespace WebApplication1
{
public partial class _default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
WebrequesProxy();
}
protected void WebrequesProxy()
{
WebRequest wrGETURL;
wrGETURL = WebRequest.Create(TextBox1.Text);
WebProxy myProxy = new WebProxy("http://winproxy.server.lan:3128/",true);
wrGETURL.Proxy = myProxy;
Stream objStream;
objStream = wrGETURL.GetResponse().GetResponseStream();
StreamReader objReader = new StreamReader(objStream);
Label1.Text = objReader.ReadToEnd();
}
}
}