localhost에서 ASP.net JSON 서비스를 실행하고 있습니다. Android 앱에서 서버로 JSON POST 요청을 보내고 있습니다. 서버가 연결을 수신하고 있지만 POST 데이터가 없습니다 (Android 앱에서 POST 후 발생하는 중단 점을 설정하여이를 확인합니다). 내가 얻은 HttpURLConnection 응답 코드는 200 OK입니다. 그러나 데이터는 서버에서 수신되지 않습니다. 데이터가 전송되고 있는지 확실하지 않습니다. 내 안드로이드 코드는 (AsyncTask에 래핑되어 있음) :
public void makeHttpRequest(String verb, String serverAddr, String postBody)
{
HttpURLConnection urlConnection = null;
OutputStream out = null;
try {
serverAddr = "http://10.0.2.2:4617/parent/dummy";
URL url = new URL(serverAddr);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setDoOutput(true);
urlConnection.setChunkedStreamingMode(0);
urlConnection.setRequestProperty("Transfer-Encoding","chunked");
urlConnection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
urlConnection.connect();
out = new BufferedOutputStream(urlConnection.getOutputStream());
out.write(postBody.getBytes("UTF-8"));
int responseCode = urlConnection.getResponseCode();
System.out.println("HTTP Response Code: " + responseCode + " | " + urlConnection.getResponseMessage());
}
catch (MalformedURLException mal) {
//
}
catch (IOException ioe) {
//
}
finally {
if (out != null) {
try {
out.close();
} catch(IOException e) {
}
}
if (urlConnection != null)
urlConnection.disconnect();
}
}
C # / ASP.NET 서비스 계약. Parent의 인스턴스가 null이지만 POST에서 보낸 데이터를 포함해야합니다.
[ServiceContract]
public interface IRestServiceParent
{
[WebInvoke(UriTemplate = "{dummy}", Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
Parent PostParent(string dummy, Parent instance);
}
public Parent PostParent(string dummy, Parent instance)
{
var result = MasterStorageStore.Instance.PostParent(instance);
if (result == null) return emptyParent;
return NewCopy(result);
}
내가 설정에도 불구하고 어떤 이유로하는 간단한 PHP 스크립트를 통해 발견 setRequestMode("POST")
하고 setDoOuput(true)
, HttpURLConnection
로 전송 받고 GET
대신 POST
. 이유를 모르겠습니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다