You are viewing a plain text version of this content. The canonical link for it is here.
Posted to solr-user@lucene.apache.org by Vineet Mishra <cl...@gmail.com> on 2013/10/30 10:50:15 UTC

Making a Web Request is failing with 403 Request Forbidden

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 = "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=
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!

Re: Making a Web Request is failing with 403 Request Forbidden

Posted by Alexandre Rafalovitch <ar...@gmail.com>.
On Wed, Oct 30, 2013 at 4:50 PM, Vineet Mishra <cl...@gmail.com>wrote:

>
> 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.
>


This does not seem to be a Solr question. Perhaps look at more generic web
request tracing tools like Wireshark, etc to compare valid and failing
request.

If this is Solr related, please narrow this down to Solr aspect of the
problem.

Regards,
   Alex.

Personal website: http://www.outerthoughts.com/
LinkedIn: http://www.linkedin.com/in/alexandrerafalovitch
- Time is the quality of nature that keeps events from happening all at
once. Lately, it doesn't seem to be working.  (Anonymous  - via GTD book)