Appache HTTP Components 라이브러리는 일반 웹 사이트와 통신하는 데는 문제가 없지만 웹 서비스에는 연결할 수 없습니다.아파치 HttpClient로 웹 서비스에 연결
Jersey 프레임 워크를 사용하여 Web Service-REST와 통신 할 Java Desktop 응용 프로그램 내에서 실행되는 구성 요소를 빌드 중입니다. 웹 서비스가 실행 중이고 웹 브라우저 및 netbeans의 Test-Web-Service 기능과 통신 할 수 있습니다.
일반 웹 페이지 또는 내 컴퓨터에서 실행중인 Glassfish 웹 서버의 기본 페이지에서 읽으려고하면 클라이언트 코드가 제대로 작동합니다. 클라이언트 프로그램 (아래 코드)을 사용하여 웹 서비스에 접속하려고하면 웹 서버에서 404 찾을 수 없음 오류가 발생합니다.
import java.io.*;
import org.apache.http.*;
import org.apache.http.client.*;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.client.methods.*; //HttpHead, HttpPut, HttpGet, etc...
import org.apache.http.util.EntityUtils;
public class ApacheClientDemo {
public static void demo() throws IOException {
String uri = "http://localhost:8080/Proctorest/resources/helloWorld";
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(uri);
HttpResponse response = httpclient.execute(httpget);
System.out.println(response.getStatusLine().toString());
HttpEntity entity = response.getEntity();
System.out.println();
System.out.println(EntityUtils.toString(entity));
}
public static void main(String[] args) {
try {
demo();
}
catch(IOException ioe) {
System.out.println(ioe);
}
}
}
온라인 또는 로컬 호스트가 아닌 웹 서비스 사이트 일반 웹 사이트로 이동, 출력은 웹 페이지의 내용으로 이어
HTTP/1.1 200 OK
입니다.
는위의 코드는 나에게
HTTP/1.1 404 Not Found
편집을 제공합니다 다음은 웹 서비스에 대한 코드입니다. 웹 서비스 코드는 Netbeans 샘플 프로젝트에서 생성되었습니다.
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
*
* Oracle and Java are registered trademarks of Oracle and/or its affiliates.
* Other names may be trademarks of their respective owners.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common
* Development and Distribution License("CDDL") (collectively, the
* "License"). You may not use this file except in compliance with the
* License. You can obtain a copy of the License at
* http://www.netbeans.org/cddl-gplv2.html
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
* specific language governing permissions and limitations under the
* License. When distributing the software, include this License Header
* Notice in each file and include the License file at
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the GPL Version 2 section of the License file that
* accompanied this code. If applicable, add the following below the
* License Header, with the fields enclosed by brackets [] replaced by
* your own identifying information:
* "Portions Copyrighted [year] [name of copyright owner]"
*
* Contributor(s):
*
* The Original Software is NetBeans. The Initial Developer of the Original
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
* Microsystems, Inc. All Rights Reserved.
*
* If you wish your version of this file to be governed by only the CDDL
* or only the GPL Version 2, indicate your decision by adding
* "[Contributor] elects to include this software in this distribution
* under the [CDDL or GPL Version 2] license." If you do not indicate a
* single choice of license, a recipient has the option to distribute
* your version of this file under either the CDDL, the GPL Version 2 or
* to extend the choice of license to its licensees as provided above.
* However, if you add GPL Version 2 code and therefore, elected the GPL
* Version 2 license, then the option applies only if the new code is
* made subject to such option by the copyright holder.
*/
package helloworld;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Produces;
import javax.ws.rs.Consumes;
/**
* REST Web Service
*
* @author mkuchtiak
*/
@Stateless
@Path("/helloWorld")
public class HelloWorldResource {
@EJB
private NameStorageBean nameStorage;
/**
* Retrieves representation of an instance of helloworld.HelloWorldResource
* @return an instance of java.lang.String
*/
@GET
@Produces("text/html")
public String getXml() {
return "<html><body><h1>Hello "+nameStorage.getName()+"!</h1></body></html>";
}
/**
* PUT method for updating an instance of HelloWorldResource
* @param content representation for the resource
* @return an HTTP response with content of the updated or created resource.
*/
@PUT
@Consumes("text/plain")
public void putXml(String content) {
nameStorage.setName(content);
}
}
/////////// NameStorageBean .java
import javax.ejb.Singleton;
/** Singleton session bean used to store the name parameter for "/helloWorld" resource
*
* @author mkuchtiak
*/
@Singleton
public class NameStorageBean {
// name field
private String name = "World";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
당신이'HTTP를 입력 할 때 발생합니다 // localhost : 8080/Proctorest/resources/helloWorld'를 웹 브라우저에서 사용하거나 'curl'/'wget'을 사용 하시겠습니까? –
helloWorld 리소스는 @get 메서드를 구현하므로 웹 브라우저에서 예상 한 페이지를 제공합니다. 정말 질문입니다. Glassfish가 웹 브라우저 요청에 응답하지만 Java 클라이언트 요청에 응답하지 않는 이유는 무엇입니까? – Thorn