C#에서 간단한 WCF Restful 웹 서비스를 작성하여 일부 매개 변수에 대한 값을 얻습니다. 여기 내 IService 코드 :HttpUrlConnection을 통해 Android에서 WCF Restful Service를 사용합니다. 오류 405- 메소드가 허용되지 않습니다.
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped)]
string GetURL(string pVr);
그리고 이것은 Service.svc
에 대한 코드public string GetURL(string pVr)
{
try
{
string VersionCode = ConfigurationManager.AppSettings["MobAppVersionCode"];
if (Convert.ToInt32(VersionCode) > Convert.ToInt32(pVr))
{
return "http://xxx.xxx.xxx.xxx/abc.apk";
}
else
{
return "No apk available";
}
}
catch (Exception ex)
{
throw ex;
}
}
내 목적 중 하나 방법이 숨겨져 야 할 IIS에 나는이 웹 서비스를 게시 할 때입니다 (이다 내가 웹 설정을 변경하고 난 쉽게 우체부를 통해 또는하여 서비스 결과를 볼 수 있습니다 IIS에이 웹 서비스를 게시 한 후 지금이
<behaviors>
<serviceBehaviors>
<behavior name="SEILServiceBehaviour">
<serviceMetadata httpGetEnabled="false" httpsGetEnabled="false" />
<serviceDebug includeExceptionDetailInFaults="false" httpHelpPageEnabled="false" httpsHelpPageEnabled="false" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp helpEnabled="false"/>
</behavior>
</endpointBehaviors>
</behaviors>
같이 설정할 수 있도록) 나머지 또는 도움말 페이지 또는 WSDL 더 이상 없다 웹 브라우저 또는 심지어 Retrofit2 android 라이브러리를 통해 데이터를 가져올 수 있습니다. 그러나이 웹 서비스를 HttpUrlConnection 코드와 연결하면 오류 405 - Method Not Allowed가 표시됩니다. 여기 내 연결 페이지의 코드는
public void onRequest(String urlParameters) {
try {
//Establishing connection with particular url
URL url = new URL("http://xxx.xxx.xxx.xxx/Service.svc/GetURL");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//Setting connection timeout
connection.setReadTimeout(30000);
connection.setConnectTimeout(30000);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.getBytes().length));
connection.setRequestProperty("Content-Language", "en-US");
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
InputStream inputStream;
int status = connection.getResponseCode();
if (status != HttpURLConnection.HTTP_OK)
inputStream = connection.getErrorStream();
else
inputStream = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(inputStream));
String line;
StringBuilder response = new StringBuilder();
while ((line = rd.readLine()) != null) {
response.append(line);
response.append('\r');
}
rd.close();
if (url.getHost().equals(connection.getURL().getHost())) {
urlReceive = response.toString().trim();
} else {
urlReceive = "Internet Login Required";
}
} catch (IOException e) {
urlReceive = "Connection Timed Out";
}
}
나에게
도와주세요입니다
우편 배달부에서 GET을 사용했습니다. 완벽한 결과를 얻었습니다. 안드로이드 retrofit에서 나는 또한 (어떤 GET 또는 POST를 사용하지 않아도) 완벽한 결과를 얻었습니다. – MaAc
나중에 우리는 안드로이드 코드에서 웹 서비스에 대한 몇 가지 매개 변수를 게시하므로 일부 값을 얻을 수 있습니다. 그러나 나는 또한 안드로이드 측면을 사용하려고했습니다. 여기에 허용되지 않는 에러가 발생합니다. – MaAc
예. 게시자가이 API를 게시 할 때 오류 405- 메소드가 허용되지 않습니다. 즉, 모바일에서 GET 호출을해야한다는 것입니다. 그러나 어떻게 해야할지 모르겠다. – MaAc