ANNOUNCEMENT: Live Wireshark University & Allegro Packets online APAC Wireshark Training Session
April 17th, 2024 | 14:30-16:00 SGT (UTC+8) | Online

Wireshark-users: [Wireshark-users] Making a Web Request is failing with 403 Request Forbidden

From: Vineet Mishra <clearmidoubt@xxxxxxxxx>
Date: Wed, 30 Oct 2013 16:11:16 +0530
Hi All,

I am making web server call to a website for Shortening the links, that is bit.ly but recieving a 403 Request Forbidden.
Although if I use their webpage to short the web link its working good.
Can any body tell me what might be the reason for such a vague behavior.

Here is the code included.

String url = "" href="https://bitly.com/shorten/" target="_blank">https://bitly.com/shorten/";
StringBuffer response;
try {
URL obj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
 
//add reuqest header
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.22 (KHTML, like Gecko) Ubuntu Chromium/25.0.1364.160 Chrome/25.0.1364.160 Safari/537.22");
con.setRequestProperty("Accept-Language", "en-US,en;q=0.8");
con.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
con.setRequestProperty("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.3");
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
con.setRequestProperty("Host", "bitly.com");

String urlParameters = "url="" href="http://bit.ly/1f3aLrP&ie=utf-8&oe=utf-8&gws_rd=cr&ei=sKlwUvPbN8j-rAf-5IDwAQ&basic_style=1&classic_mode=&rapid_shorten_mode=&_xsrf=a2b71eaf499c4690a77a21d3c87e6302" target="_blank">http://bit.ly/1f3aLrP&ie=utf-8&oe=utf-8&gws_rd=cr&ei=sKlwUvPbN8j-rAf-5IDwAQ&basic_style=1&classic_mode=&rapid_shorten_mode=&_xsrf=a2b71eaf499c4690a77a21d3c87e6302";
 
// Send post request
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
 
int responseCode = con.getResponseCode();
System.out.println("Response Code : " + responseCode);
 
BufferedReader in = new BufferedReader(
       new InputStreamReader(con.getInputStream()));
String inputLine;
response = new StringBuffer();
 
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
System.out.println(response.toString());
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Hoping for your response.

Thanks!