2012-04-07 94 views
0

我尝试写一个客户端服务器程序,在我的程序,每一轮,客户端发送两个String到服务器,第一个是网络地址和第二个约user.but一些随机的信息时,URL地址到达服务器,我得到这个异常:HTTP响应代码:403客户端发送一个URL地址时,服务器

Exception in thread "main" java.io.IOException: Server returned HTTP response code: 403 for URL: http://www.google.com.hk/search?hl=en&source=hp&q=java&gbv=2 
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1245) 
    at java.net.URL.openStream(URL.java:1009) 
    at server.Server.processClient(Dom.java:124) 
    at server.Server.run(Dom.java:90) 
    at server.Server.main(Dom.java:155) 
Java Result: 1 

,但我不知道为什么得到一个禁止访问响应。

客户端

   private static BufferedWriter toServer; 
.... 

    public void progressUpdated(NavigatorProgressEvent npe) { 



      Matcher matcher = pattern.matcher (npe.getUrl().toString()); 


      if (matcher.matches()) 
         { 
       System.out.println(npe.getUrl()); 
         toServer.println(npe.getUrl().toString()); 


           } 

服务器端

... 
    url = new URL(fromClient.readLine()); 

....

+0

请把你的源代码了。 – dash1e 2012-04-07 05:04:27

+0

@ dash1e查看更新 – lonesome 2012-04-07 05:08:30

回答

0

也许你在你的头的请求错过 “用户代理”。

尝试添加以下代码:如果没有指定用户代理名

httpUrlConnection.setRequestProperty("User-Agent","MyUserAgentName"); 

谷歌没有回应。并且尽量选择一个良好的用户代理名:d

+0

这是什么?你能解释一下吗? – lonesome 2012-04-07 05:07:56

+0

你使用'HttpUrlConnection'吗? – dash1e 2012-04-07 05:09:39

+0

是我,因为你可以在堆栈跟踪看到以及添加 – lonesome 2012-04-07 05:11:41

相关问题