Re: [WP8 ] POST 訊息到網頁

看板WindowsPhone作者 (寒江雪)時間12年前 (2014/03/06 19:56), 編輯推噓1(109)
留言10則, 2人參與, 最新討論串2/3 (看更多)
今天測試後又發現了一個狀況,如果送出的目標網址,只是單純回應訊息, 就可以正常回應,但是如果是要接受參數的網址,只要不是在本機的網站, 都會回應500。所以應該是MultipartFormDataContent我有漏了什麼設定, 導致目標網頁無法正常解析嗎? 以下這是我的作法 頁面程式碼 string url = "”; MultipartFormDataContent param = new MultipartFormDataContent(); HttpContent loginContent = new StringContent(UserLogin.Text); HttpContent PasswordContent = new StringContent(UserPwd.Text); loginContent.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded"); PasswordContent.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded"); param.Add(loginContent, "login"); param.Add(PasswordContent, "Password"); string userdata = await Model.getRequest.PostDataToWeb(url,param); PostDataToWeb函數如下 public static async Task<string> PostDataToWeb(string url, MultipartFormDataContent content) { var tcs = new TaskCompletionSource<string>(); var httpClient = new HttpClient(new HttpClientHandler()); string responseString=""; try { HttpResponseMessage response = await httpClient.PostAsync(url, content); response.EnsureSuccessStatusCode(); responseString = await response.Content.ReadAsStringAsync(); } catch (Exception ex) { tcs.TrySetException(ex); return ex.Message; } return responseString; } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 118.169.75.78

03/06 20:13, , 1F
port訊息的部分,url+參數可以嗎?
03/06 20:13, 1F

03/06 20:13, , 2F
另外上篇文章有推文 不知道有沒有看過~?
03/06 20:13, 2F

03/06 20:42, , 3F
恩~有看到了~但似乎還是沒有用
03/06 20:42, 3F

03/06 20:43, , 4F
URL+參數的方是好像可以~因為我上面說的單純回應訊息的
03/06 20:43, 4F

03/06 20:43, , 5F
就是有代參數
03/06 20:43, 5F

03/06 21:08, , 6F
不知道在server上的事件檢視器 看不看的出來哪邊有問題
03/06 21:08, 6F

03/06 21:31, , 7F
看了上篇推文 突然在想會不會是遠端port擋住了
03/06 21:31, 7F

03/06 22:35, , 8F
應該不是~因為我上篇用的第一種方法~可以成功
03/06 22:35, 8F

03/06 22:36, , 9F
除非兩種不同的方法走不同port?
03/06 22:36, 9F
補一下我第一種方法~主要就是上色地方不同~也就是參數的建置方式 函式部分 public static async Task<string> PostDataToWeb(string url, List<KeyValuePair<string, string>> content) { var tcs = new TaskCompletionSource<string>(); var httpClient = new HttpClient(new HttpClientHandler()); string responseString=""; try { HttpResponseMessage response = await httpClient.PostAsync(url, new FormUrlEncodedContent(content)); response.EnsureSuccessStatusCode(); responseString = await response.Content.ReadAsStringAsync(); } catch (Exception ex) { tcs.TrySetException(ex); return ex.Message; } return responseString; } ※ 編輯: dalconan 來自: 118.169.75.78 (03/06 22:41)

03/06 22:50, , 10F
因為我實際上沒用過 所以也沒辦法給予解法~上MSDN問問吧
03/06 22:50, 10F
文章代碼(AID): #1J669qwu (WindowsPhone)
文章代碼(AID): #1J669qwu (WindowsPhone)