403通过Java应用程序调用服务时给出的响应,但不是通过浏览器调用时

时间:2017-01-02 10:30:23

标签: java web-services rest

我开发了一种常见的post方法来将帖子请求发送到服务。

private final   String post(String path,String requestBody){
    String uriStr = "http://208.112.83.44:84/api"+path;
    String json = null;

     try {

         URL url = new URL(uriStr);

         HttpURLConnection conn =    (HttpURLConnection) url.openConnection(); 

            byte[]postData = requestBody.trim().getBytes(StandardCharsets.UTF_8);
            Integer postDataLength = postData.length;
            conn.setRequestMethod("POST");
            conn.setDoOutput(true);
            conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
            conn.setRequestProperty("content-type", "application/json");
            conn.setRequestProperty("Authorization", "QkJDQzVCMkEtMEJBNS0055676UE5ODMtMjJEOUVGODQ2RjdC");
            conn.setRequestProperty("x-ApplicationId", "bbcc5b2a-99099-a983-22d9ef846f7b");
            conn.setRequestProperty("charset", "utf-8");
            conn.setFixedLengthStreamingMode(postDataLength);
            OutputStream wr = conn.getOutputStream(); 
            wr.write(postData);
            wr.flush();
            wr.close();
            InputStream input = conn.getInputStream();
            BufferedReader in = new BufferedReader(new InputStreamReader(input));
            String inputLine;
            StringBuffer response = new StringBuffer();
            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            json = response.toString();
            in.close();


        } catch (IOException ex) {
            ex.printStackTrace();
        }
    return json;

}

我将post方法称为

this.post("/authentication","{"Email":"laskum@gmail.com","Password":"Lasa@123"}");

但是我得到了例外。

java.io.IOException: Server returned HTTP response code: 403 for URL: http://208.112.83.44:84/api/authentication
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1840)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1441)
at com.sabrered.prs.model.PRSRestModel.post(PRSRestModel.java:91)
at com.sabrered.prs.model.PRSRestModel.log(PRSRestModel.java:283)
at unitTest.TestAuthentication.testLog(TestAuthentication.java:33)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

我提到了许多与相同异常相关的帖子,其中大多数都提到了将User Agent添加到请求的解决方案,因为我已添加如下

conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");

我仍然得到同样的例外。当我尝试通过浏览器成功发送请求并使用200状态代码响应时。

请帮我一个有任何想法的人。看看我的代码吧。

1 个答案:

答案 0 :(得分:0)

我对问题进行了排序。在调用post方法之前,需要使用端口启动代理。

所以我添加了以下内容。

WebServiceUtil.initSocksProxy("6789");