Reputation: 13
I am trying to consume a web service. The response is "Request Rejected.The requested URL was rejected. Please consult with your administrator.Your support ID is: 11127005317486483617"
This is my code:
import java.io.BufferedReader;<br/>
import java.io.IOException;<br/>
import java.io.InputStreamReader;<br/>
import java.io.StringReader;<br/>
import java.net.HttpURLConnection;<br/>
import java.net.MalformedURLException;<br/>
import java.net.URL;<br/>
import java.util.*;<br/>
import java.io.FileNotFoundException;<br/>
import java.io.FileReader;<br/>
import java.io.IOException;<br/>
import org.json.JSONArray;<br/>
import org.json.JSONException;<br/>
import java.util.Iterator;<br/>
import org.json.JSONObject;<br/>
import org.json.simple.parser.JSONParser;<br/>
import org.json.simple.parser.ParseException;<br/>
public class Testapp {<br/>
public static void main(String[] args) throws JSONException, ParseException {<br/>
String output = "abc";<br/>
try {<br/>
URL url = new URL("https://peps.cnes.fr/resto/api/collections/S1/search.json?q=france&maxRecords=100&startDate=2014-12-31T05:00:00&completionDate=2014-12-31T12:00:00");<br/>
HttpURLConnection conn = (HttpURLConnection) url.openConnection();<br/>
conn.setRequestMethod("GET");<br/>
conn.setRequestProperty("Accept", "application/json");<br/>
if (conn.getResponseCode() != 200) {<br/>
throw new RuntimeException("Failed : HTTP error code : "<br/>
+ conn.getResponseCode());<br/>
}<br/>
BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));<br/>
//String output;<br/>
System.out.println("Output from Server .... \n");<br/>
while ((output = br.readLine()) != null) {<br/>
System.out.println(output);<br/>
}<br/>
conn.disconnect();<br/>
} catch (MalformedURLException e) {<br/>
e.printStackTrace();<br/>
} catch (IOException e) {<br/>
e.printStackTrace();<br/>
}<br/>
}<br/>
}<br/>
But,this url worked well in browser. Somebody help me?
Upvotes: 1
Views: 1624
Reputation: 33000
Many web sites require a User-Agent
header in order to work properly.
So add
conn.setRequestProperty("User-Agent", "Mozilla/5.0");
to make this work.
Upvotes: 3